Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Drag & drop in a QTableView: which flags do I need in the model?

    General and Desktop
    4
    14
    803
    Loading More Posts
    • 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.
    • R
      Robert Hairgrove last edited by

      The model should not be editable, in the sense that users should not be allowed to type in their own values. But I would like to be able to drag an item from one column into the other. So in the model class, presumably I should NOT set the flag Qt::ItemIsEditable but only Qt::ItemIsDropEnabled, is this correct? Of course, I should implement setData() in my model.

      I also need to ensure that only items from the same row can be dropped into other columns. I assume that I would need to derive a custom class from QAbstractTableView and overload the dragEnterEvent() in order to record which row this was called upon?

      JonB raven-worx 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Robert Hairgrove last edited by JonB

        @robert-hairgrove
        While you await a better answer: what you write seems reasonable to me (though I've never used!)

        Meanwhile, have a read through https://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views I think might have lots of ideas for you,

        1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators @Robert Hairgrove last edited by

          @robert-hairgrove said in Drag & drop in a QTableView: which flags do I need in the model?:

          So in the model class, presumably I should NOT set the flag Qt::ItemIsEditable but only Qt::ItemIsDropEnabled, is this correct?

          yes

          I also need to ensure that only items from the same row can be dropped into other columns. I assume that I would need to derive a custom class from QAbstractTableView and overload the dragEnterEvent() in order to record which row this was called upon?

          better QAbstractItemModel::canDropMimeData()

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          JonB 1 Reply Last reply Reply Quote 2
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            @JonB the page is here.

            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 Reply Quote 2
            • JonB
              JonB @raven-worx last edited by JonB

              @raven-worx

              better QAbstractItemModel::canDropMimeData()

              That looks good for the destination. How does he know what the source row/column was? That has nothing to do with MIME type.

              raven-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @JonB last edited by

                @jonb said in Drag & drop in a QTableView: which flags do I need in the model?:

                How does he know what the source row/column was?

                either reimplement a custom mimeData() method or decode the data yourself (which might not be the best idea since the encoding might change in future Qt versions)

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @raven-worx last edited by JonB

                  @raven-worx
                  (Bearing in mind I've never actually done any of this)
                  I'm lost with the MIME data stuff, conceptually. What OP seems to want is to use a drag operation in the view to cause the model to copy data from one QModelIndex to another. To me there shouldn't be any "data to decode"? I just want to know the QModelIndexes of the start and end of the drag-drop?

                  raven-worx 1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators @JonB last edited by

                    @jonb said in Drag & drop in a QTableView: which flags do I need in the model?:

                    To me there shouldn't be any "data to decode"?

                    either use the data which is already provided (as it is encoded into a bytestream) or add custom data when you want custom behavior. The data can contain anything you like and can easily added.
                    This needs to be done anyway, no matter where the drop is handled.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    JonB 1 Reply Last reply Reply Quote 1
                    • JonB
                      JonB @raven-worx last edited by

                      @raven-worx
                      OK. I read up. In https://doc.qt.io/qt-5/qmimedata.html#details

                      If the drag and drop operation occurs within a single application, we can subclass QMimeData and add extra data in it, and use a qobject_cast() in the receiver's drop event handler. For example:

                      void MyWidget::dropEvent(QDropEvent *event)
                      {
                          const MyMimeData *myData =
                                  qobject_cast<const MyMimeData *>(event->mimeData());
                          if (myData) {
                              // access myData's data directly (not through QMimeData's API)
                          }
                      }
                      

                      So this is how OP will pass information about the QModelIndex of the source, and be able to check its row against the destination's, right?

                      raven-worx 1 Reply Last reply Reply Quote 0
                      • raven-worx
                        raven-worx Moderators @JonB last edited by

                        @jonb
                        no, no need to subclass QMimeData.
                        QMimeData can hold arbitrary data already. So a custom mime-type can be used for example.

                        mimeData->setData("my/data", data)
                        

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply Reply Quote 2
                        • R
                          Robert Hairgrove last edited by Robert Hairgrove

                          @raven-worx : Thank you for the suggestion to use a custom MIME type. I also never did this but once, many years ago, but with a much simpler use case.

                          If I understand correctly, in addition to my own model class, I will need to subclass QTableView (since there is no QAbstractTableView, but only QAbstractTableModel) and re-implement the startDrag() function? That seems to be the only place where I can set the MIME data. Then I can let the model handle everything else, I guess.

                          One more question: Can I use multiple MIME types, calling QMimeData::setData() for each type? This seems to be the easiest to implement. Otherwise, I would probably encode it in JSON format.

                          1 Reply Last reply Reply Quote 0
                          • R
                            Robert Hairgrove last edited by

                            Actually, I see now that it is the function QAbstractItemModel::mimeData() that has to be re-implemented because the default implementation of QAbstractItemView::startDrag() calls this function for each item (i.e. QModelIndex) which is selected for dragging.

                            So there is no need for a custom view ... the model handles everything itself?

                            raven-worx 1 Reply Last reply Reply Quote 0
                            • raven-worx
                              raven-worx Moderators @Robert Hairgrove last edited by

                              @robert-hairgrove said in Drag & drop in a QTableView: which flags do I need in the model?:

                              the model handles everything itself?

                              yes

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              R 1 Reply Last reply Reply Quote 2
                              • R
                                Robert Hairgrove @raven-worx last edited by

                                @raven-worx : Thanks very much!

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post