Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag and Drop?
Qt 6.11 is out! See what's new in the release blog

Drag and Drop?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Orangeatang
    wrote on last edited by
    #1

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

    @
    myUI.setupUi(this);
    gridLayout = new QGridLayout( myUI.myAudioSourceTab );
    gridLayout->setSpacing(6);
    gridLayout->setContentsMargins(11, 11, 11, 11);
    gridLayout->setObjectName(QStringLiteral("g"));
    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):

    !http://i.imgur.com/tM0iUND.png(drag/drop)!

    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?

    Cheers!

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Orangeatang
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Hi,

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

        157

        1 Reply Last reply
        0
        • O Offline
          O Offline
          Orangeatang
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Try setting
            @
            setDragDropMode(QAbstractItemView::DragDrop);
            @

            157

            1 Reply Last reply
            0
            • O Offline
              O Offline
              Orangeatang
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  Orangeatang
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      Orangeatang
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        Orangeatang
                        wrote on last edited by
                        #11

                        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?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved