Drag and Drop of files
Drag and Drop of files on QMainWindows
Background / technical stuff
Drag and drop is done via some events:
- void dragEnterEvent(QDragEnterEvent* event);
- void dragMoveEvent(QDragMoveEvent* event);
- void dragLeaveEvent(QDragLeaveEvent* event);
- void dropEvent(QDropEvent* event);
They are called in order to let the application decide if it could accept a drag/drop or not. If the application says: do not accept this dragEnter event, then the OS will deny dropping of the content for the application. Same applies to the dragMoveEvent (perhaps, dropping is only allowed in special places, like toolbar etc.).
The dropp event is called, when the user drops his data. Inside the drop event, the widget must react on the different drop actions and the content type. e.g. (here we react on all actions):
Use the DocumentWindow class
If you want to create an (MDI) editor application, you can derive your main window class from DocumentWindow instead of QMainWindow. If you implement an MDI application, you should also reimplement the virtual method
ddeOpenFile
.
The example code is based on the Qt MDI Example [doc.qt.nokia.com]
The only changes needed are:
- derive from DocumentWindow instead of QMainWindow
- implement the functions virtual bool openFiles(const QStringList& pathList);
Use the DocumentWindow sources
DocumentWindow.h
DocumentWindow.cpp
You may use this code without any warranty.
Future extensions / combinations
typically I would use this together with a file type assignment [developer.qt.nokia.com] on windows
The code of the class and a demo project are located on gitorious in the following repository: qtdevnet-dropfiles [gitorious.org]
Versions
The current code is version 1 of the code.
Version 1
This marks the first public release.
Feedback
I would really welcome feedback on this class. If you spot big weaknesses, please let me know.