Drag and Drop of files: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Snippets]] | |||
==Background / technical stuff== | [toc align_right="yes" depth="3"] | ||
= Drag and Drop of files on QMainWindows = | |||
== Background / technical stuff == | |||
Drag and drop is done via some events: | Drag and drop is done via some events: | ||
Line 14: | Line 18: | ||
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): | 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): | ||
<code><br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event->mimeData(); | |||
// check for our needed mime type, here a file or a list of files<br /> if (mimeData->hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&lt;QUrl&gt; urlList = mimeData->urls(); | |||
The only changes needed are: | // extract the local paths of the files<br /> for (int i = 0; i < urlList.size() && i < 32; +''i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;#40;&#41;);<br /> } | ||
<br /> // call a function to open the files<br /> openFiles(pathList);<br /> }<br />}<br /></code> | |||
<br />h2. Use the DocumentWindow class | |||
<br />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 <code&gt;ddeOpenFile&lt;/code&gt;.<br />The example code is based on the "Qt MDI Example&quot;:http://doc.qt.nokia.com/4.7/mainwindows-mdi.html | |||
<br />The only changes needed are: | |||
<br />* derive from DocumentWindow instead of QMainWindow<br />* implement the functions virtual bool openFiles(const QStringList&amp; pathList); | |||
<br /><code><br />bool MainWindow::openFiles(const QStringList&amp; pathList)<br />{<br /> bool success = true;<br /> for (int i = 0; i < pathList.size() && i < 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))<br /> {<br /> statusBar()<s>>showMessage(tr("File loaded&quot;), 2000);<br /> child</s>>show();<br /> }<br /> else<br /> {<br /> child->close();<br /> }<br /> }<br /> return success;<br />}<br /></code> | |||
<br />h2. Use the DocumentWindow sources | |||
<br />h3. DocumentWindow.h | |||
<br /><code><br />// ————————————————————————————————-<br />/'''*<br /> * </code>file<br /> * <code>brief<br /> * </code>author Gerolf Reinwardt<br /> * <code>date 30. march 2011<br /> *<br /> * Copyright © 2011, Gerolf Reinwardt. All rights reserved.<br /> *<br /> * Simplified BSD License<br /> *<br /> * Redistribution and use in source and binary forms, with or without modification, are<br /> * permitted provided that the following conditions are met:<br /> *<br /> * 1. Redistributions of source code must retain the above copyright notice, this list of<br /> * conditions and the following disclaimer.<br /> *<br /> * 2. Redistributions in binary form must reproduce the above copyright notice, this list<br /> * of conditions and the following disclaimer in the documentation and/or other materials<br /> * provided with the distribution.<br /> *<br /> * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER&gt; ``AS IS'' AND ANY EXPRESS OR IMPLIED<br /> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND<br /> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER&gt; OR<br /> * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br /> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br /> * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON<br /> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br /> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br /> * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br /> *<br /> * The views and conclusions contained in the software and documentation are those of the<br /> * authors and should not be interpreted as representing official policies, either expressed<br /> * or implied, of Gerolf Reinwardt.<br /> '''/<br />// ————————————————————————————————- | |||
<br />#ifndef WIDGET_H<br />#define WIDGET_H | |||
<br />// —— general includes —————————————————————————<br />#include <QtGui/QMainWindow&gt;<br />#include <QtCore/QStringList&gt; | |||
<br />// —— local includes —————————————————————————-<br />// —— pre defines ——————————————————————————- | |||
<br />// —— class definition —————————————————————————<br />/'''*<br /> * </code>short<br /> *<br /> * The usage is fairly easy. Derive your own MainWindow class from DocumentWindow instead of QMainWindow<br /> * and implement the pure virtual function openFiles.<br /> *<br /> * <code>code<br /> MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags) :<br /> DocumentWindow(parent, flags)<br /> {<br /> … | |||
<br /> setWindowTitle(tr("MDI&quot;));<br /> setUnifiedTitleAndToolBarOnMac(true);<br /> }<br /> </code>endcode<br /> *<br /> * Aditionally, the openFiles must be implermented:<br /> *<br /> * <code>code<br /> bool MyClass::openFiles(const QStringList&amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i < pathList.size() && i < 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))<br /> {<br /> statusBar()<s>>showMessage(tr("File loaded&quot;), 2000);<br /> child</s>>show();<br /> }<br /> else<br /> {<br /> child->close();<br /> }<br /> }<br /> return success;<br /> }<br /> </code>endcode<br />'''<br /> '''/<br />class DocumentWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:<br /> // —— enums ———————————————————————————<br /> // —— construction —————————————————————————<br /> /'''*<br /> * Constructor.<br /> *<br /> * Creates a DocumentWindow with a given <code>arg parent and <code>arg flags.<br /> '''/<br /> explicit DocumentWindow(QWidget''' parent = 0, Qt::WindowFlags flags = 0); | |||
<br /> /**<br /> * Destructor<br /> '''/<br /> ~DocumentWindow(); | |||
<br /> // —— operators ——————————————————————————<br /> // —— methods ——————————————————————————-<br /> // —— accessors ——————————————————————————<br /> // —— members ——————————————————————————- | |||
<br />protected:<br /> // —— events ———————————————————————————<br /> /'''*<br /> * this event is called when the mouse enters the widgets area during a drag/drop operation<br /> '''/<br /> void dragEnterEvent(QDragEnterEvent''' event); | |||
<br /> /**<br /> * this event is called when the mouse moves inside the widgets area during a drag/drop operation<br /> '''/<br /> void dragMoveEvent(QDragMoveEvent''' event); | |||
<br /> /**<br /> * this event is called when the mouse leaves the widgets area during a drag/drop operation<br /> '''/<br /> void dragLeaveEvent(QDragLeaveEvent''' event); | |||
<br /> /**<br /> * this event is called when the drop operation is initiated at the widget<br /> '''/<br /> void dropEvent(QDropEvent''' event); | |||
<br /> // —— helpers ——————————————————————————-<br /> /**<br /> * This method must be implemented by the client. It is used to opened the dropped files.<br /> *<br /> * </code>param pathList list of urls given by the drop event<br /> * <code>retval true if successfull otherwise false<br /> *<br /> * Here is an example implementation:<br /> *<br /> * </code>code<br /> bool MyClass::openFiles(const QStringList&amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i < pathList.size() && i < 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))<br /> {<br /> statusBar()<s>>showMessage(tr("File loaded&quot;), 2000);<br /> child</s>>show();<br /> }<br /> else<br /> {<br /> child->close();<br /> }<br /> }<br /> return success;<br /> }<br /> <code>endcode<br />'''/<br /> virtual bool openFiles(const QStringList&amp; pathList) = 0; | |||
<br />private:<br /> // —— privat helpers ————————————————————————<br /> // —— members ——————————————————————————-<br /> // —— not allowed members ——————————————————————-<br />}; | |||
<br />#endif // WIDGET_H<br /></code> | |||
<br />h3. DocumentWindow.cpp | |||
<br /><code><br />// ————————————————————————————————-<br />/**<br /> * </code>file<br /> * <code>brief<br /> * </code>author Gerolf Reinwardt<br /> * <code>date 30.01.2011<br /> *<br /> * Copyright © 2011, Gerolf Reinwardt. All rights reserved.<br /> *<br /> * Simplified BSD License<br /> *<br /> * Redistribution and use in source and binary forms, with or without modification, are<br /> * permitted provided that the following conditions are met:<br /> *<br /> * 1. Redistributions of source code must retain the above copyright notice, this list of<br /> * conditions and the following disclaimer.<br /> *<br /> * 2. Redistributions in binary form must reproduce the above copyright notice, this list<br /> * of conditions and the following disclaimer in the documentation and/or other materials<br /> * provided with the distribution.<br /> *<br /> * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER&gt; ``AS IS'' AND ANY EXPRESS OR IMPLIED<br /> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND<br /> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER&gt; OR<br /> * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br /> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br /> * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON<br /> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br /> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br /> * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br /> *<br /> * The views and conclusions contained in the software and documentation are those of the<br /> * authors and should not be interpreted as representing official policies, either expressed<br /> * or implied, of Gerolf Reinwardt.<br /> '''/<br />// ————————————————————————————————- | |||
<br />// —— general includes —————————————————————————<br />#include <QtGui/QDragEnterEvent&gt;<br />#include <QtGui/QDragLeaveEvent&gt;<br />#include <QtGui/QDragMoveEvent&gt;<br />#include <QtGui/QDropEvent&gt;<br />#include <QtCore/QMimeData&gt;<br />#include <QtCore/QUrl&gt;<br />#include <QtCore/QList&gt; | |||
<br />// —— local includes —————————————————————————-<br />#include "documentwindow.h&quot; | |||
<br />// —— construction ——————————————————————————<br />DocumentWindow::DocumentWindow(QWidget''' parent, Qt::WindowFlags flags) :<br /> QMainWindow(parent, flags)<br />{<br /> setAcceptDrops(true);<br />} | |||
* | <br />DocumentWindow::~DocumentWindow()<br />{<br />} | ||
<br />// —— operators ———————————————————————————<br />// —— methods ————————————————————————————<br />// —— accessors ———————————————————————————<br />// —— public slots ——————————————————————————<br />// —— protected slots —————————————————————————<br />// —— events ————————————————————————————<br />void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)<br />{<br /> // if some actions should not be usable, like move, this code must be adopted<br /> event->acceptProposedAction();<br />} | |||
<br />void DocumentWindow::dragMoveEvent(QDragMoveEvent* event)<br />{<br /> // if some actions should not be usable, like move, this code must be adopted<br /> event->acceptProposedAction();<br />} | |||
== | <br />void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)<br />{<br /> event->accept();<br />} | ||
<br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event->mimeData(); | |||
<br /> if (mimeData->hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&lt;QUrl&gt; urlList = mimeData->urls(); | |||
<br /> for (int i = 0; i < urlList.size() && i < 32;''+i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;#40;&#41;);<br /> } | |||
if(openFiles(pathList))<br /> event->acceptProposedAction();<br /> }<br />} | |||
// —— private slots ——————————————————————————<br />// —— private helpers —————————————————————————<br /></code> | |||
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 | typically I would use this together with a "file type assignment&quot;:http://developer.qt.nokia.com/wiki/Assigning_a_file_type_to_an_Application_on_Windows on windows | ||
The code of the class and a demo project are located on gitorious in the following repository: | The code of the class and a demo project are located on gitorious in the following repository: "qtdevnet-dropfiles&quot;:https://www.gitorious.org/qtdevnet-wiki-mvc/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. | ||
Revision as of 14:41, 23 February 2015
[toc align_right="yes" depth="3"]
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):
<br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event->mimeData();
// check for our needed mime type, here a file or a list of files<br /> if (mimeData->hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&lt;QUrl&gt; urlList = mimeData->urls();
// extract the local paths of the files<br /> for (int i = 0; i < urlList.size() && i < 32; +''i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;#40;&#41;);<br /> }
<br /> // call a function to open the files<br /> openFiles(pathList);<br /> }<br />}<br />
h2. 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 <code>ddeOpenFile</code>.
The example code is based on the "Qt MDI Example":http://doc.qt.nokia.com/4.7/mainwindows-mdi.html
The only changes needed are:
* derive from DocumentWindow instead of QMainWindow
* implement the functions virtual bool openFiles(const QStringList& pathList);
<br />bool MainWindow::openFiles(const QStringList&amp; pathList)<br />{<br /> bool success = true;<br /> for (int i = 0; i < pathList.size() && i < 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))<br /> {<br /> statusBar()<s>>showMessage(tr("File loaded&quot;), 2000);<br /> child</s>>show();<br /> }<br /> else<br /> {<br /> child->close();<br /> }<br /> }<br /> return success;<br />}<br />
h2. Use the DocumentWindow sources
h3. DocumentWindow.h
<br />// ————————————————————————————————-<br />/'''*<br /> *
file
*
brief<br /> *
author Gerolf Reinwardt
*
date 30. march 2011<br /> *<br /> * Copyright © 2011, Gerolf Reinwardt. All rights reserved.<br /> *<br /> * Simplified BSD License<br /> *<br /> * Redistribution and use in source and binary forms, with or without modification, are<br /> * permitted provided that the following conditions are met:<br /> *<br /> * 1. Redistributions of source code must retain the above copyright notice, this list of<br /> * conditions and the following disclaimer.<br /> *<br /> * 2. Redistributions in binary form must reproduce the above copyright notice, this list<br /> * of conditions and the following disclaimer in the documentation and/or other materials<br /> * provided with the distribution.<br /> *<br /> * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER&gt; ``AS IS'' AND ANY EXPRESS OR IMPLIED<br /> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND<br /> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER&gt; OR<br /> * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br /> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br /> * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON<br /> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br /> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br /> * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br /> *<br /> * The views and conclusions contained in the software and documentation are those of the<br /> * authors and should not be interpreted as representing official policies, either expressed<br /> * or implied, of Gerolf Reinwardt.<br /> '''/<br />// ————————————————————————————————-
<br />#ifndef WIDGET_H<br />#define WIDGET_H
<br />// —— general includes —————————————————————————<br />#include <QtGui/QMainWindow&gt;<br />#include <QtCore/QStringList&gt;
<br />// —— local includes —————————————————————————-<br />// —— pre defines ——————————————————————————-
<br />// —— class definition —————————————————————————<br />/'''*<br /> *
short
*
* The usage is fairly easy. Derive your own MainWindow class from DocumentWindow instead of QMainWindow
* and implement the pure virtual function openFiles.
*
*
code<br /> MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags) :<br /> DocumentWindow(parent, flags)<br /> {<br /> …
<br /> setWindowTitle(tr("MDI&quot;));<br /> setUnifiedTitleAndToolBarOnMac(true);<br /> }<br />
endcode
*
* Aditionally, the openFiles must be implermented:
*
*
code<br /> bool MyClass::openFiles(const QStringList&amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i < pathList.size() && i < 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))<br /> {<br /> statusBar()<s>>showMessage(tr("File loaded&quot;), 2000);<br /> child</s>>show();<br /> }<br /> else<br /> {<br /> child->close();<br /> }<br /> }<br /> return success;<br /> }<br />
endcode
/
class DocumentWindow : public QMainWindow
{
Q_OBJECT
public:
// —— enums ———————————————————————————
// —— construction —————————————————————————
/*
* Constructor.
*
* Creates a DocumentWindow with a given
arg parent and <code>arg flags.<br /> '''/<br /> explicit DocumentWindow(QWidget''' parent = 0, Qt::WindowFlags flags = 0);
<br /> /**<br /> * Destructor<br /> '''/<br /> ~DocumentWindow();
<br /> // —— operators ——————————————————————————<br /> // —— methods ——————————————————————————-<br /> // —— accessors ——————————————————————————<br /> // —— members ——————————————————————————-
<br />protected:<br /> // —— events ———————————————————————————<br /> /'''*<br /> * this event is called when the mouse enters the widgets area during a drag/drop operation<br /> '''/<br /> void dragEnterEvent(QDragEnterEvent''' event);
<br /> /**<br /> * this event is called when the mouse moves inside the widgets area during a drag/drop operation<br /> '''/<br /> void dragMoveEvent(QDragMoveEvent''' event);
<br /> /**<br /> * this event is called when the mouse leaves the widgets area during a drag/drop operation<br /> '''/<br /> void dragLeaveEvent(QDragLeaveEvent''' event);
<br /> /**<br /> * this event is called when the drop operation is initiated at the widget<br /> '''/<br /> void dropEvent(QDropEvent''' event);
<br /> // —— helpers ——————————————————————————-<br /> /**<br /> * This method must be implemented by the client. It is used to opened the dropped files.<br /> *<br /> *
param pathList list of urls given by the drop event
*
retval true if successfull otherwise false<br /> *<br /> * Here is an example implementation:<br /> *<br /> *
code
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&#40;pathList.at(i&#41;))
{
statusBar()>showMessage(tr("File loaded"), 2000);>show();
child
}
else
{
child->close();
}
}
return success;
}
endcode<br />'''/<br /> virtual bool openFiles(const QStringList&amp; pathList) = 0;
<br />private:<br /> // —— privat helpers ————————————————————————<br /> // —— members ——————————————————————————-<br /> // —— not allowed members ——————————————————————-<br />};
<br />#endif // WIDGET_H<br />
h3. DocumentWindow.cpp
<br />// ————————————————————————————————-<br />/**<br /> *
file
*
brief<br /> *
author Gerolf Reinwardt
*
date 30.01.2011<br /> *<br /> * Copyright © 2011, Gerolf Reinwardt. All rights reserved.<br /> *<br /> * Simplified BSD License<br /> *<br /> * Redistribution and use in source and binary forms, with or without modification, are<br /> * permitted provided that the following conditions are met:<br /> *<br /> * 1. Redistributions of source code must retain the above copyright notice, this list of<br /> * conditions and the following disclaimer.<br /> *<br /> * 2. Redistributions in binary form must reproduce the above copyright notice, this list<br /> * of conditions and the following disclaimer in the documentation and/or other materials<br /> * provided with the distribution.<br /> *<br /> * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER&gt; ``AS IS'' AND ANY EXPRESS OR IMPLIED<br /> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND<br /> * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER&gt; OR<br /> * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br /> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br /> * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON<br /> * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING<br /> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br /> * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br /> *<br /> * The views and conclusions contained in the software and documentation are those of the<br /> * authors and should not be interpreted as representing official policies, either expressed<br /> * or implied, of Gerolf Reinwardt.<br /> '''/<br />// ————————————————————————————————-
<br />// —— general includes —————————————————————————<br />#include <QtGui/QDragEnterEvent&gt;<br />#include <QtGui/QDragLeaveEvent&gt;<br />#include <QtGui/QDragMoveEvent&gt;<br />#include <QtGui/QDropEvent&gt;<br />#include <QtCore/QMimeData&gt;<br />#include <QtCore/QUrl&gt;<br />#include <QtCore/QList&gt;
<br />// —— local includes —————————————————————————-<br />#include "documentwindow.h&quot;
<br />// —— construction ——————————————————————————<br />DocumentWindow::DocumentWindow(QWidget''' parent, Qt::WindowFlags flags) :<br /> QMainWindow(parent, flags)<br />{<br /> setAcceptDrops(true);<br />}
<br />DocumentWindow::~DocumentWindow()<br />{<br />}
<br />// —— operators ———————————————————————————<br />// —— methods ————————————————————————————<br />// —— accessors ———————————————————————————<br />// —— public slots ——————————————————————————<br />// —— protected slots —————————————————————————<br />// —— events ————————————————————————————<br />void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)<br />{<br /> // if some actions should not be usable, like move, this code must be adopted<br /> event->acceptProposedAction();<br />}
<br />void DocumentWindow::dragMoveEvent(QDragMoveEvent* event)<br />{<br /> // if some actions should not be usable, like move, this code must be adopted<br /> event->acceptProposedAction();<br />}
<br />void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)<br />{<br /> event->accept();<br />}
<br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event->mimeData();
<br /> if (mimeData->hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&lt;QUrl&gt; urlList = mimeData->urls();
<br /> for (int i = 0; i < urlList.size() && i < 32;''+i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;#40;&#41;);<br /> }
if(openFiles(pathList))<br /> event->acceptProposedAction();<br /> }<br />}
// —— private slots ——————————————————————————<br />// —— private helpers —————————————————————————<br />
You may use this code without any warranty.
Future extensions / combinations
typically I would use this together with a "file type assignment":http://developer.qt.nokia.com/wiki/Assigning_a_file_type_to_an_Application_on_Windows on windows
The code of the class and a demo project are located on gitorious in the following repository: "qtdevnet-dropfiles":https://www.gitorious.org/qtdevnet-wiki-mvc/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.