Drag and Drop of files: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Don't #include the module prefix)
(Reformatted the code without actually changing it.)
Line 19: Line 19:
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>
  void DocumentWindow::dropEvent(QDropEvent* event)
void DocumentWindow::dropEvent(QDropEvent* event)
  {
{
    const QMimeData* mimeData = event->mimeData();
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);
    }
  }


// check for our needed mime type, here a file or a list of files
== Use the DocumentWindow class ==
if (mimeData->hasUrls())
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
{
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
  ddeOpenFile
openFiles(pathList);
}
}
</code>


== 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 [http://doc.qt.nokia.com/4.7/mainwindows-mdi.html Qt MDI Example]
The example code is based on the [http://doc.qt.nokia.com/4.7/mainwindows-mdi.html Qt MDI Example]


Line 51: Line 52:
* implement the functions virtual bool openFiles(const QStringList& pathList);
* implement the functions virtual bool openFiles(const QStringList& pathList);


<code>
  bool MainWindow::openFiles(const QStringList& pathList)
bool MainWindow::openFiles(const QStringList& pathList)
  {
{
    bool success = true;
bool success = true;
    for (int i = 0; i < pathList.size() && i < 32;i)
for (int i = 0; i < pathList.size() && i < 32;i)
    {
{
      MdiChild '''child = createMdiChild();
MdiChild '''child = createMdiChild();
      if (child->loadFile(pathList.at(i)))
if (child->loadFile(pathList.at(i)))
      {
{
        statusBar()->showMessage(tr("File loaded"), 2000);
statusBar()->showMessage(tr("File loaded"), 2000);
        child->show();
child->show();
      }
}
      else
else
      {
{
        child->close();
child->close();
      }
}
    }
}
    return success;
return success;
  }
}
</code>


== Use the DocumentWindow sources ==
== Use the DocumentWindow sources ==
=== DocumentWindow.h ===
=== DocumentWindow.h ===
<code>
// ————————————————————————————————-
/'''*
* </code>file
* <code>brief
* </code>author Gerolf Reinwardt
* <code>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 —————————————————————————
/'''*
* </code>short
*
* The usage is fairly easy. Derive your own MainWindow class from DocumentWindow instead of QMainWindow
* and implement the pure virtual function openFiles.
*
* <code>code
MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags) :
DocumentWindow(parent, flags)
{
setWindowTitle(tr("MDI"));
setUnifiedTitleAndToolBarOnMac(true);
}
</code>endcode
*
* Aditionally, the openFiles must be implermented:
*
* <code>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(pathList.at(i)))
{
statusBar()->showMessage(tr("File loaded"), 2000);
child->show();
}
else
{
child->close();
}
}
return success;
}
</code>endcode
'''
*/
class DocumentWindow : public QMainWindow
{
Q_OBJECT
public:
// —— enums ———————————————————————————
// —— construction —————————————————————————
/'''*
* Constructor.
*
* Creates a DocumentWindow with a given <code>arg parent and <code>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.
*
* </code>param pathList list of urls given by the drop event
* <code>retval true if successfull otherwise false
*
* Here is an example implementation:
*
* </code>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(pathList.at(i)))
{
statusBar()->showMessage(tr("File loaded"), 2000);
child->show();
}
else
{
child->close();
}
}
return success;
}
<code>endcode
*/
virtual bool openFiles(const QStringList& pathList) = 0;
private:
// —— privat helpers ————————————————————————
// —— members ——————————————————————————-
// —— not allowed members ——————————————————————-
};


#endif // WIDGET_H
  // ————————————————————————————————-
</code>
  /*
  * 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 ===
=== DocumentWindow.cpp ===
<code>
  // ————————————————————————————————-
// ————————————————————————————————-
  /**
/**
  * author Gerolf Reinwardt
* </code>file
  * date 30.01.2011
* <code>brief
  *
* </code>author Gerolf Reinwardt
  * Copyright © 2011, Gerolf Reinwardt. All rights reserved.
* <code>date 30.01.2011
  *
*
  * Simplified BSD License
* Copyright © 2011, Gerolf Reinwardt. All rights reserved.
  *
*
  * Redistribution and use in source and binary forms, with or without modification, are
* Simplified BSD License
  * permitted provided that the following conditions are met:
*
  *
* Redistribution and use in source and binary forms, with or without modification, are
  * 1. Redistributions of source code must retain the above copyright notice, this list of
* permitted provided that the following conditions are met:
  * conditions and the following disclaimer.
*
  *
* 1. Redistributions of source code must retain the above copyright notice, this list of
  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
* conditions and the following disclaimer.
  * of conditions and the following disclaimer in the documentation and/or other materials
*
  * provided with the distribution.
* 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
  * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
* provided with the distribution.
  * 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
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 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
  * The views and conclusions contained in the software and documentation are those of the
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * authors and should not be interpreted as representing official policies, either expressed
*
  * or implied, of Gerolf Reinwardt.
* 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>
// —— general includes —————————————————————————
  #include <QDragMoveEvent>
#include <QDragEnterEvent>
  #include <QDropEvent>
#include <QDragLeaveEvent>
  #include <QMimeData>
#include <QDragMoveEvent>
  #include <QUrl>
#include <QDropEvent>
  #include <QList>
#include <QMimeData>
 
#include <QUrl>
  // —— local includes —————————————————————————-
#include <QList>
  #include "documentwindow.h"
 
 
// —— local includes —————————————————————————-
  // —— construction ——————————————————————————
#include "documentwindow.h"
  DocumentWindow::DocumentWindow(QWidget''' parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
 
  {
 
    setAcceptDrops(true);
// —— construction ——————————————————————————
  }
DocumentWindow::DocumentWindow(QWidget''' parent, Qt::WindowFlags flags) :
 
QMainWindow(parent, flags)
  DocumentWindow::~DocumentWindow()
{
  {
setAcceptDrops(true);
  }
}
 
 
  // —— operators ———————————————————————————
DocumentWindow::~DocumentWindow()
  // —— methods ————————————————————————————
{
  // —— accessors ———————————————————————————
}
  // —— public slots ——————————————————————————
 
  // —— protected slots —————————————————————————
// —— operators ———————————————————————————
  // —— events ————————————————————————————
// —— methods ————————————————————————————
  void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)
// —— accessors ———————————————————————————
  {
// —— public slots ——————————————————————————
    // if some actions should not be usable, like move, this code must be adopted
// —— protected slots —————————————————————————
    event->acceptProposedAction();
// —— events ————————————————————————————
  }
void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)
 
{
  void DocumentWindow::dragMoveEvent(QDragMoveEvent* event)
// if some actions should not be usable, like move, this code must be adopted
  {
event->acceptProposedAction();
    // if some actions should not be usable, like move, this code must be adopted
}
    event->acceptProposedAction();
 
  }
void DocumentWindow::dragMoveEvent(QDragMoveEvent* event)
 
{
  void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)
// if some actions should not be usable, like move, this code must be adopted
  {
event->acceptProposedAction();
    event->accept();
}
  }
 
 
 
  void DocumentWindow::dropEvent(QDropEvent* event)
void DocumentWindow::dragLeaveEvent(QDragLeaveEvent* event)
  {
{
    const QMimeData* mimeData = event->mimeData();
event->accept();
 
}
    if (mimeData->hasUrls())
 
    {
void DocumentWindow::dropEvent(QDropEvent* event)
      QStringList pathList;
{
      QList<QUrl> urlList = mimeData->urls();
const QMimeData* mimeData = event->mimeData();
 
 
      for (int i = 0; i < urlList.size() && i < 32;''+i)
if (mimeData->hasUrls())
      {
{
        pathList.append(urlList.at(i).toLocalFile());
QStringList pathList;
      }
QList<QUrl> urlList = mimeData->urls();
 
 
      if(openFiles(pathList))
for (int i = 0; i < urlList.size() && i < 32;''+i)
        event->acceptProposedAction();
{
    }
pathList.append(urlList.at(i).toLocalFile());
  }
}
 
 
  // —— private slots ——————————————————————————
if(openFiles(pathList))
  // —— private helpers —————————————————————————
event->acceptProposedAction();
}
}
 
// —— private slots ——————————————————————————
// —— private helpers —————————————————————————
</code>


You may use this code without any warranty.
You may use this code without any warranty.

Revision as of 19:22, 2 March 2016

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 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):

 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.