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.

Event handlers

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:

  • WheatDragLabel, PotatoDragLabel, CowDragLabel, SheepDragLabel:
    • mouseMoveEvent
  • FieldGraphicsScene:
    • dragEnterEvent
    • dragMoveEvent
    • dropEvent

However, before we do that we must consider another factor - MIME types.

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:

  • image/jpeg
  • text/html
  • video/mp4

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:

  • application/x-wheat
  • application/x-potato
  • application/x-cow
  • application/x-sheep

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.