Drag and Drop?

  • Hey folks,

    I'm trying to get drag/drop support running for an application I'm building, but must be missing something with the initialisation of the widget.

    I'm building a class derived from QTreeView, and have overridden all of the suggest functions :

    void dragEnterEvent( QDragEnterEvent *anEvent ) override;
    void dragMoveEvent( QDragMoveEvent *anEvent ) override;
    void dragLeaveEvent( QDragLeaveEvent *anEvent ) override;
    void dropEvent( QDropEvent *anEvent ) override;

    I've also made sure that the constructor for the widget sets 'accepts drops' to true :

    AudioSourceTreeView::AudioSourceTreeView( QWidget* aParent ) : QTreeView( aParent )
    setAcceptDrops( true );
    setFrameStyle( QFrame::Sunken | QFrame::StyledPanel );

    I'm adding an instance of the widget to a grid programmatically at runtime (this is taken from the constructor of my QMainWindow instance):

    gridLayout = new QGridLayout( myUI.myAudioSourceTab );
    gridLayout->setContentsMargins(11, 11, 11, 11);
    myUI.myAudioSourceTab->setAcceptDrops( true );

    myAudioSourceTreeView = new AudioSourceTreeView( myUI.myAudioSourceTab );
    myAudioSourceTreeView->setObjectName( QStringLiteral("myAudioSourceTreeView") );
    gridLayout->addWidget( myAudioSourceTreeView, 0, 0, 1, 1 );

    But whenever I try and drag/drop a file in to the tree view (I'm also not hitting any of the dragEnter/dropEvent callbacks):


    I've also tried overriding the drag/drop events in the QMainWindow class (and calling setAcceptDrops) in the hopes of sending the events through to child widgets... but I get the same result.

    Any ideas what I might be missing here?


  • I've tried testing the DropSite example included with Qt 5.3.1, and that works without any issues.

    There are very few differences between the two projects, but I'm using Visual Studio 2012 (Qt 1.2.3 integration). I guess it's possible I'm missing a module or a project setting somewhere?

  • Moderators


    Can you check if "this":https://qt-project.org/forums/viewthread/28509 post would work for you ?

  • Hi p3c0,

    I checked out the post you linked to, and unfortunately it didn't seem to make any difference... I tried overriding

    bool dropMimeData(...)


    setDragDropMode( QAbstractItemView::InternalMove );

    But I'm not getting any callbacks when I drag a file over the application windows (I still get the black circle with a strike-through icon), this includes dragEnterEvent, dragMoveEvent & dragLeaveEvent.

    I don't think it's a problem with the widget implementation though - pretty sure it's coming from a higher level in Qt because I tried deriving my base class from QLabel, with the same result.

  • Moderators

    Try setting

  • No luck - I've tried most of the drag drop modes without any favorable results.

    It must be coming from a higher level that the QWidget that I'm deriving from though; if I derive the base class from QLabel instead of QTreeWidget/QTreeView I get the exact same behaviour.

  • Lifetime Qt Champion


    Did you look at the "item view dedicated":http://qt-project.org/doc/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views drag and drop documentation ?

    Hope it helps

  • Hi SGaist,

    I had a look at the item view link you posted, but no luck unfortunately... setting the viewport & drop indicators doesn't make any difference to the application.

    There has to be something broken at a higher level in my application, since deriving from QLabel (like in the Qt DropBox example) doesn't work either.

  • Lifetime Qt Champion

    Just to be sure, did you try to run one of the example to ensure it's working correctly ?

  • I did, yeah (DropZone) - but from the Qt Creator application. I'm going to try and create the UI programmatically to see if that makes any difference.

  • I've been messing around with this, trying to get drag and drop working - and the problem seems to be coming from the way the project is set up by the VS2012 integration.

    If I create a completely empty project, link to the Qt libs and derive my main window class from either QWidget or QMainWindow everything works without any issues (assuming I call setAcceptsDrops(true) ). This does present a problem though, since I don't get the auto generated metadata if I use the Q_OBJECT macro in my class definitions.

    If I use the Qt5 Project templates when I create the project, I get all of the metatdata generation for free... but drag/drop just doesn't work (using the same source code from the empty project). Whenever I drag a file from windows explorer over the application I get the invalid operation icon (shown in the first post in the thread).

    Any ideas?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.