Important: Please read the Qt Code of Conduct -

Drag and drop .exe to create icon bar

  • What I am trying to do is make a quick launch bar in one of my applications that can spawn an application. What I would like to do is be able to drag a application executable into a QFrame (which is into a vbox layout for the icon buttons) and recognize the drop. Then add the image of the executable into the bar.

    I think from there it would be easy to create a QToolButton that spawns a QProcess when the application button is clicked in icon bar.

    I guess I am looking for some guidance on how I would go about doing the drag and drop and the image processing of the executable. Or if there is something similar out there...

    Thank you in advance!

    Edit: from what I have read so far, I have set up a subclass of a QFrame in hope of being able to have a drag and drop event. Still unsure of the best way to do this though...

  • You just have to re-implement dragEnterEvent() and dropEvent() in your widget in order to accept and process files that have been dropped onto your application via Drag&Drop. If a file gets dropped onto the application (e.g. from Windows Explorer), you can get the path to that file. In my application I use code like this:


    • File dragged over window
      void MainWindow::dragEnterEvent(QDragEnterEvent *event)
      QStringList formats = event->mimeData()->formats();

    /accept drag if suitable/
    if(formats.contains("application/x-qt-windows-mime;value="FileNameW"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))


    • File dropped onto window
      void MainWindow::dropEvent(QDropEvent *event)
      QStringList droppedFiles;
      QList<QUrl> urls = event->mimeData()->urls();

    QUrl currentUrl = urls.takeFirst();
    QFileInfo file(currentUrl.toLocalFile());
    droppedFiles << file.canonicalFilePath();

    /* do something with the dropped files */

    I don't think you can get an Icon from the dropped file directly. But once you have path to the file, you could use the QFileIconProvider class to get a QIcon for that file...

  • Perfect! This is awesome stuff! So I guess I have another question, can the drag drop event be used on any object by subclassing?

    I think I am going to implement it into a QFrame with a vbox layout to they will align when dragged in.

  • dragEnterEvent, dropEvent etc. are methods of QWidget, so in principle, you can use that everywhere. Some of the more advanced widgets provide some default implementation for drag/drop with some more advanced API. The item view classes like [[Doc:QTreeView]] or [[Doc:QTextEdit]] come into mind here.

Log in to reply