Drag and Drop of files: Difference between revisions
Slava Kamhin (talk | contribs) (added * symbol to drag and drop methods, because event parameter should be a pointer) |
m (removed gender from a pronoun) |
||
Line 4: | Line 4: | ||
= Drag and Drop of files on QMainWindows = | =Drag and Drop of files on QMainWindows= | ||
== Background / technical stuff == | ==Background / technical stuff== | ||
Drag and drop is done via some events: | Drag and drop is done via some events: | ||
* void dragEnterEvent(QDragEnterEvent* event); | *void dragEnterEvent(QDragEnterEvent* event); | ||
* void dragMoveEvent(QDragMoveEvent* event); | *void dragMoveEvent(QDragMoveEvent* event); | ||
* void dragLeaveEvent(QDragLeaveEvent* event); | *void dragLeaveEvent(QDragLeaveEvent* event); | ||
* void dropEvent(QDropEvent* 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.). | 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 | The drop event is called when the user drops their 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): | ||
void DocumentWindow::dropEvent(QDropEvent* event) | void DocumentWindow::dropEvent(QDropEvent* event) | ||
Line 40: | Line 40: | ||
} | } | ||
== Use the DocumentWindow class == | ==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 | 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 | ||
Line 49: | Line 49: | ||
The only changes needed are: | The only changes needed are: | ||
* derive from DocumentWindow instead of QMainWindow | *derive from DocumentWindow instead of QMainWindow | ||
* implement the functions virtual bool openFiles(const QStringList& pathList); | *implement the functions virtual bool openFiles(const QStringList& pathList); | ||
bool MainWindow::openFiles(const QStringList& pathList) | bool MainWindow::openFiles(const QStringList& pathList) | ||
Line 71: | Line 71: | ||
} | } | ||
== Use the DocumentWindow sources == | ==Use the DocumentWindow sources== | ||
=== DocumentWindow.h === | ===DocumentWindow.h=== | ||
// ————————————————————————————————- | // ————————————————————————————————- | ||
Line 234: | Line 234: | ||
#endif WIDGET_H | #endif WIDGET_H | ||
=== DocumentWindow.cpp === | ===DocumentWindow.cpp=== | ||
// ————————————————————————————————- | // ————————————————————————————————- | ||
/** | /** | ||
Line 339: | Line 339: | ||
You may use this code without any warranty. | You may use this code without any warranty. | ||
== Future extensions / combinations == | ==Future extensions / combinations== | ||
typically I would use this together with a [http://developer.qt.nokia.com/wiki/Assigning_a_file_type_to_an_Application_on_Windows file type assignment] on windows | typically I would use this together with a [http://developer.qt.nokia.com/wiki/Assigning_a_file_type_to_an_Application_on_Windows file type assignment] on windows | ||
Line 345: | Line 345: | ||
The code of the class and a demo project are located on gitorious in the following repository: [https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-dropfiles qtdevnet-dropfiles] | The code of the class and a demo project are located on gitorious in the following repository: [https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-dropfiles qtdevnet-dropfiles] | ||
== Versions == | ==Versions== | ||
The current code is version 1 of the code. | The current code is version 1 of the code. | ||
==== Version 1 ==== | ====Version 1==== | ||
This marks the first public release. | This marks the first public release. | ||
== Feedback == | ==Feedback== | ||
I would really welcome feedback on this class. If you spot big weaknesses, please let me know. | I would really welcome feedback on this class. If you spot big weaknesses, please let me know. |
Latest revision as of 06:31, 1 June 2023
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
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 drop event is called when the user drops their 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):
void DocumentWindow::dropEvent(QDropEvent* event) { const QMimeData* mimeData = event->mimeData(); // check for our needed mime type, here a file or a list of files if (mimeData->hasUrls()) { QStringList pathList; QList<QUrl> urlList = mimeData->urls(); // extract the local paths of the files for (int i = 0; i < urlList.size() && i < 32; +i) { pathList.append(urlList.at(i).toLocalFile()); } // call a function to open the files openFiles(pathList); } }
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
The only changes needed are:
- derive from DocumentWindow instead of QMainWindow
- implement the functions virtual bool openFiles(const QStringList& pathList);
bool MainWindow::openFiles(const QStringList& pathList) { bool success = true; for (int i = 0; i < pathList.size() && i < 32;i) { MdiChild child = createMdiChild(); if (child->loadFile(pathList.at(i))) { statusBar()->showMessage(tr("File loaded"), 2000); child->show(); } else { child->close(); } } return success; }
Use the DocumentWindow sources
DocumentWindow.h
// ————————————————————————————————- /* * author Gerolf Reinwardt * date 30. march 2011 * * Copyright © 2011, Gerolf Reinwardt. All rights reserved. * * Simplified BSD License * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of Gerolf Reinwardt. */ // ————————————————————————————————- #ifndef WIDGET_H #define WIDGET_H // —— general includes ————————————————————————— #include <QMainWindow> #include <QStringList> // —— local includes —————————————————————————- // —— pre defines ——————————————————————————- // —— class definition ————————————————————————— /* * The usage is fairly easy. Derive your own MainWindow class from DocumentWindow instead of QMainWindow * and implement the pure virtual function openFiles. * */ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags) : DocumentWindow(parent, flags) { setWindowTitle(tr("MDI")); setUnifiedTitleAndToolBarOnMac(true); } /* * Aditionally, the openFiles must be implermented: */ bool MyClass::openFiles(const QStringList& pathList) { bool success = true; for (int i = 0; i < pathList.size() && i < 32;i) { MdiChild child = createMdiChild(); if (child->loadFile(pathList.at(i))) { statusBar()->showMessage(tr("File loaded"), 2000); child->show(); } else { child->close(); } } return success; } class DocumentWindow : public QMainWindow { Q_OBJECT public: // —— enums ——————————————————————————— // —— construction ————————————————————————— /* * Constructor. * * Creates a DocumentWindow with a given arg parent and arg flags. */ explicit DocumentWindow(QWidget parent = 0, Qt::WindowFlags flags = 0); /** * Destructor */ ~DocumentWindow(); // —— operators —————————————————————————— // —— methods ——————————————————————————- // —— accessors —————————————————————————— // —— members ——————————————————————————- protected: // —— events ——————————————————————————— /* * this event is called when the mouse enters the widgets area during a drag/drop operation */ void dragEnterEvent(QDragEnterEvent* event); /** * this event is called when the mouse moves inside the widgets area during a drag/drop operation */ void dragMoveEvent(QDragMoveEvent* event); /** * this event is called when the mouse leaves the widgets area during a drag/drop operation */ void dragLeaveEvent(QDragLeaveEvent* event); /** * this event is called when the drop operation is initiated at the widget */ void dropEvent(QDropEvent* event); // —— helpers ——————————————————————————- /** * This method must be implemented by the client. It is used to opened the dropped files. * param pathList list of urls given by the drop event * retval true if successfull otherwise false * * Here is an example implementation: */ bool MyClass::openFiles(const QStringList& pathList) { bool success = true; for (int i = 0; i < pathList.size() && i < 32;i) { MdiChild child = createMdiChild(); if (child->loadFile(pathList.at(i))) { statusBar()->showMessage(tr("File loaded"), 2000); child->show(); } else { child->close(); } } return success; } virtual bool openFiles(const QStringList& pathList) = 0; private: // —— privat helpers ———————————————————————— // —— members ——————————————————————————- // —— not allowed members ——————————————————————- }; #endif WIDGET_H
DocumentWindow.cpp
// ————————————————————————————————- /** * author Gerolf Reinwardt * date 30.01.2011 * * Copyright © 2011, Gerolf Reinwardt. All rights reserved. * * Simplified BSD License * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of Gerolf Reinwardt. */ // ————————————————————————————————- // —— general includes ————————————————————————— #include <QDragEnterEvent> #include <QDragLeaveEvent> #include <QDragMoveEvent> #include <QDropEvent> #include <QMimeData> #include <QUrl> #include <QList> // —— local includes —————————————————————————- #include "documentwindow.h" // —— construction —————————————————————————— DocumentWindow::DocumentWindow(QWidget parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { setAcceptDrops(true); } DocumentWindow::~DocumentWindow() { } // —— operators ——————————————————————————— // —— methods ———————————————————————————— // —— accessors ——————————————————————————— // —— public slots —————————————————————————— // —— protected slots ————————————————————————— // —— events ———————————————————————————— void DocumentWindow::dragEnterEvent(QDragEnterEvent* event) { // if some actions should not be usable, like move, this code must be adopted event->acceptProposedAction(); } void DocumentWindow::dragMoveEvent(QDragMoveEvent* event) { // if some actions should not be usable, like move, this code must be adopted event->acceptProposedAction(); } void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event) { event->accept(); } void DocumentWindow::dropEvent(QDropEvent* event) { const QMimeData* mimeData = event->mimeData(); if (mimeData->hasUrls()) { QStringList pathList; QList<QUrl> urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < 32;+i) { pathList.append(urlList.at(i).toLocalFile()); } if(openFiles(pathList)) event->acceptProposedAction(); } } // —— private slots —————————————————————————— // —— private helpers —————————————————————————
You may use this code without any warranty.
Future extensions / combinations
typically I would use this together with a file type assignment on windows
The code of the class and a demo project are located on gitorious in the following repository: qtdevnet-dropfiles
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.