Introduction to event handling
We have toolbars with crop and animal icons and we also have our graphical representation of a field. Now we need to consider how to get the crops from the toolbar and into the field! There are a couple of ways we could do this:
- Clicking on the crop icons causes a crop to magically appear in the field
- Dragging the crop icon from the toolbar and into the field
We will focus on drag and drop as this is what our interface design asked for.
In order to recognise a drag and drop our objects need to be set-up with the appropriate event handlers which will perform the actions required to drag the icon and also accept the dropping of the icon on the field.
The video provides an overview of event handlers.
Which event handlers?
In order to get drag and drop working we will need to add the following event handlers to their respective objects:
However, before we do that we must consider another factor - MIME types.
When we are dragging and dropping data from one place to another, the receiving object needs to know what type of data is being dropped. It gets this information from the dropped object's MIME type. Standard objects have set MIME types such as:
The MIME type is made up from two parts:
- The type
- The sub-type
Because our QDragLabel is not a standard object will will need to define a custom MIME type for it. Our MIME types will be as follows:
This tells the receiving object that the dropped object is application data and that it has a custom sub-type of wheat, potato, cow or sheep. The 'x' prefix for our sub-types is a convention used to represent custom types.
As an aside, MIME stands for Multipurpose Internet Mail Extension and can find out more about it on Wikipedia.
- Previous - Toolbars
- Next - Adding the drag functionality to the toolbar labels
- 1. Installing PyQt on Mac OS X
- 2. Installing PyQt on Windows
- 3. Introduction to PyQt
- 4. Graphical crop simulation
- 5. Creating a basic PyQt application
- 6. Introduction to Layouts
- 7. Radio Button Widget
- 8. Creating the layouts
- 9. Switching layouts
- 10. Adding growth functionality
- 11. Graphics and resources
- 12. Summary
- 13. Field simulation
- 14. Designing the user interface
- 15. Graphical representation of crops and animals
- 16. Resources file
- 17. Crop and animal graphical sub-classes
- 18. Graphical representation of the field
- 19. Toolbars
- 20. Introduction to event handling
- 21. Adding the drag functionality to the toolbar labels
- 22. Adding the 'drop' functionality to the field
- 23. Context Menus
- 24. Automatically growing the field
- 25. Manually growing the field
- 26. Field Report
- 27. Final Summary
- 28. Distributing your application
- 29. Distributing your application on Mac OS X
- 30. Distributing your application on Windows