Accepting mouse drag and drop events
In this project example, if you drag and drop a .wav
file on a SoundEffectWidget
, you can change the sound played. The constructor of SoundEffectWidget
performs a specific task to allow drag and drop:
setAcceptDrops(true);
We can now override the drag and drop callbacks. Let's start with the dragEnterEvent()
function:
//SoundEffectWidget.h class SoundEffectWidget : public QWidget { ... protected: void dragEnterEvent(QDragEnterEvent* event) override; ... }; //SoundEffectWidget.cpp void SoundEffectWidget::dragEnterEvent(QDragEnterEvent* event) { if (event->mimeData()->hasFormat("text/uri-list")) { event->acceptProposedAction(); } }
The dragEnterEvent()
function is called each time the user drags an object on the widget. In our case, we only want to allow drag and drop on files that are of the MIME type: "text/uri-list"
(a list of URIs, which can be file://
, http...