Difference between revisions of "Drag and Drop of files"

From Qt Wiki
Jump to: navigation, search
 
Line 1: Line 1:
=Drag and Drop of files on QMainWindows=
+
[[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):
  
==Use the DocumentWindow class==
+
<code><br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event-&gt;mimeData();
  
If you want to create an (<span class="caps">MDI</span>) editor application, you can derive your main window class from DocumentWindow instead of QMainWindow. If you implement an <span class="caps">MDI</span> application, you should also reimplement the virtual method <code>ddeOpenFile</code>.<br /> The example code is based on the [http://doc.qt.nokia.com/4.7/mainwindows-mdi.html Qt <span class="caps">MDI</span> Example] ''[doc.qt.nokia.com]''
+
// check for our needed mime type, here a file or a list of files<br /> if (mimeData-&gt;hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&amp;lt;QUrl&amp;gt; urlList = mimeData-&gt;urls();
  
The only changes needed are:
+
// extract the local paths of the files<br /> for (int i = 0; i &lt; urlList.size() &amp;&amp; i &lt; 32; +''i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;amp;#40;&amp;#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 &lt;code&amp;gt;ddeOpenFile&amp;lt;/code&amp;gt;.<br />The example code is based on the &quot;Qt MDI Example&amp;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;amp; pathList);
 +
<br /><code><br />bool MainWindow::openFiles(const QStringList&amp;amp; pathList)<br />{<br /> bool success = true;<br /> for (int i = 0; i &lt; pathList.size() &amp;&amp; i &lt; 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child-&gt;loadFile&amp;amp;#40;pathList.at(i&amp;amp;#41;))<br /> {<br /> statusBar()<s>&gt;showMessage(tr(&quot;File loaded&amp;quot;), 2000);<br /> child</s>&gt;show();<br /> }<br /> else<br /> {<br /> child-&gt;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;QtGui/QMainWindow&amp;gt;<br />#include &lt;QtCore/QStringList&amp;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(&quot;MDI&amp;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;amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i &lt; pathList.size() &amp;&amp; i &lt; 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child-&gt;loadFile&amp;amp;#40;pathList.at(i&amp;amp;#41;))<br /> {<br /> statusBar()<s>&gt;showMessage(tr(&quot;File loaded&amp;quot;), 2000);<br /> child</s>&gt;show();<br /> }<br /> else<br /> {<br /> child-&gt;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;amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i &lt; pathList.size() &amp;&amp; i &lt; 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child-&gt;loadFile&amp;amp;#40;pathList.at(i&amp;amp;#41;))<br /> {<br /> statusBar()<s>&gt;showMessage(tr(&quot;File loaded&amp;quot;), 2000);<br /> child</s>&gt;show();<br /> }<br /> else<br /> {<br /> child-&gt;close();<br /> }<br /> }<br /> return success;<br /> }<br /> <code>endcode<br />'''/<br /> virtual bool openFiles(const QStringList&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;QtGui/QDragEnterEvent&amp;gt;<br />#include &lt;QtGui/QDragLeaveEvent&amp;gt;<br />#include &lt;QtGui/QDragMoveEvent&amp;gt;<br />#include &lt;QtGui/QDropEvent&amp;gt;<br />#include &lt;QtCore/QMimeData&amp;gt;<br />#include &lt;QtCore/QUrl&amp;gt;<br />#include &lt;QtCore/QList&amp;gt;
 +
<br />// —— local includes —————————————————————————-<br />#include &quot;documentwindow.h&amp;quot;
  
* derive from DocumentWindow instead of QMainWindow
+
<br />// —— construction ——————————————————————————<br />DocumentWindow::DocumentWindow(QWidget''' parent, Qt::WindowFlags flags) :<br /> QMainWindow(parent, flags)<br />{<br /> setAcceptDrops(true);<br />}
* implement the functions virtual bool openFiles(const QStringList&amp; pathList);
+
<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-&gt;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-&gt;acceptProposedAction();<br />}
  
==Use the DocumentWindow sources==
+
<br />void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)<br />{<br /> event-&gt;accept();<br />}
 +
<br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event-&gt;mimeData();
 +
<br /> if (mimeData-&gt;hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&amp;lt;QUrl&amp;gt; urlList = mimeData-&gt;urls();
 +
<br /> for (int i = 0; i &lt; urlList.size() &amp;&amp; i &lt; 32;''+i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;amp;#40;&amp;#41;);<br /> }
  
===DocumentWindow.h===
+
if(openFiles(pathList))<br /> event-&gt;acceptProposedAction();<br /> }<br />}
  
===DocumentWindow.cpp===
+
// —— 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 [http://developer.qt.nokia.com/wiki/Assigning_a_file_type_to_an_Application_on_Windows file type assignment] ''[developer.qt.nokia.com]'' on windows
+
typically I would use this together with a &quot;file type assignment&amp;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: [https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-dropfiles qtdevnet-dropfiles] ''[gitorious.org]''
+
The code of the class and a demo project are located on gitorious in the following repository: &quot;qtdevnet-dropfiles&amp;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.
 
===Categories:===
 
 
* [[:Category:snippets|snippets]]
 

Revision as of 14:41, 23 February 2015


[toc align_right="yes&quot; depth="3&quot;]

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-&gt;mimeData();

// check for our needed mime type, here a file or a list of files<br /> if (mimeData-&gt;hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&amp;lt;QUrl&amp;gt; urlList = mimeData-&gt;urls();

// extract the local paths of the files<br /> for (int i = 0; i &lt; urlList.size() &amp;&amp; i &lt; 32; +''i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;amp;#40;&amp;#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&gt;ddeOpenFile&lt;/code&gt;.
The example code is based on the "Qt MDI Example&quot;: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&amp; pathList);


<br />bool MainWindow::openFiles(const QStringList&amp;amp; pathList)<br />{<br /> bool success = true;<br /> for (int i = 0; i &lt; pathList.size() &amp;&amp; i &lt; 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child-&gt;loadFile&amp;amp;#40;pathList.at(i&amp;amp;#41;))<br /> {<br /> statusBar()<s>&gt;showMessage(tr(&quot;File loaded&amp;quot;), 2000);<br /> child</s>&gt;show();<br /> }<br /> else<br /> {<br /> child-&gt;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;QtGui/QMainWindow&amp;gt;<br />#include &lt;QtCore/QStringList&amp;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(&quot;MDI&amp;quot;));<br /> setUnifiedTitleAndToolBarOnMac(true);<br /> }<br />
endcode
*
* Aditionally, the openFiles must be implermented:
*
*
code<br /> bool MyClass::openFiles(const QStringList&amp;amp; pathList)<br /> {<br /> bool success = true;<br /> for (int i = 0; i &lt; pathList.size() &amp;&amp; i &lt; 32;i)<br /> {<br /> MdiChild '''child = createMdiChild();<br /> if (child-&gt;loadFile&amp;amp;#40;pathList.at(i&amp;amp;#41;))<br /> {<br /> statusBar()<s>&gt;showMessage(tr(&quot;File loaded&amp;quot;), 2000);<br /> child</s>&gt;show();<br /> }<br /> else<br /> {<br /> child-&gt;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&amp; pathList)
{
bool success = true;
for (int i = 0; i < pathList.size() && i < 32;i)
{
MdiChild
child = createMdiChild();
if (child->loadFile&amp;#40;pathList.at(i&amp;#41;))
{
statusBar()>showMessage(tr("File loaded&quot;), 2000);
child
>show();
}
else
{
child->close();
}
}
return success;
}
endcode<br />'''/<br /> virtual bool openFiles(const QStringList&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;COPYRIGHT HOLDER&amp;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 &lt;QtGui/QDragEnterEvent&amp;gt;<br />#include &lt;QtGui/QDragLeaveEvent&amp;gt;<br />#include &lt;QtGui/QDragMoveEvent&amp;gt;<br />#include &lt;QtGui/QDropEvent&amp;gt;<br />#include &lt;QtCore/QMimeData&amp;gt;<br />#include &lt;QtCore/QUrl&amp;gt;<br />#include &lt;QtCore/QList&amp;gt;
<br />// —— local includes —————————————————————————-<br />#include &quot;documentwindow.h&amp;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-&gt;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-&gt;acceptProposedAction();<br />}

<br />void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)<br />{<br /> event-&gt;accept();<br />}
<br />void DocumentWindow::dropEvent(QDropEvent* event)<br />{<br /> const QMimeData* mimeData = event-&gt;mimeData();
<br /> if (mimeData-&gt;hasUrls())<br /> {<br /> QStringList pathList;<br /> QList&amp;lt;QUrl&amp;gt; urlList = mimeData-&gt;urls();
<br /> for (int i = 0; i &lt; urlList.size() &amp;&amp; i &lt; 32;''+i)<br /> {<br /> pathList.append(urlList.at(i).toLocalFile&amp;amp;#40;&amp;#41;);<br /> }

if(openFiles(pathList))<br /> event-&gt;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&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: "qtdevnet-dropfiles&quot;: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.