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 & drop in a QTableView: which flags do I need in the model?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 1.9k Views 3 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on 8 Aug 2019, 15:25 last edited by
    #1

    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?

    J R 2 Replies Last reply 8 Aug 2019, 17:26
    0
    • R Robert Hairgrove
      8 Aug 2019, 15:25

      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?

      J Offline
      J Offline
      JonB
      wrote on 8 Aug 2019, 17:26 last edited by JonB 8 Aug 2019, 18:12
      #2

      @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
      0
      • R Robert Hairgrove
        8 Aug 2019, 15:25

        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?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 8 Aug 2019, 17:42 last edited by
        #3

        @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

        J 1 Reply Last reply 8 Aug 2019, 18:17
        2
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Aug 2019, 18:02 last edited by
          #4

          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
          2
          • R raven-worx
            8 Aug 2019, 17:42

            @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()

            J Offline
            J Offline
            JonB
            wrote on 8 Aug 2019, 18:17 last edited by JonB 8 Aug 2019, 18:17
            #5

            @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.

            R 1 Reply Last reply 8 Aug 2019, 18:23
            0
            • J JonB
              8 Aug 2019, 18:17

              @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.

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 8 Aug 2019, 18:23 last edited by
              #6

              @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

              J 1 Reply Last reply 8 Aug 2019, 18:37
              0
              • R raven-worx
                8 Aug 2019, 18:23

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

                J Offline
                J Offline
                JonB
                wrote on 8 Aug 2019, 18:37 last edited by JonB 8 Aug 2019, 18:39
                #7

                @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?

                R 1 Reply Last reply 8 Aug 2019, 18:45
                0
                • J JonB
                  8 Aug 2019, 18:37

                  @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?

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 8 Aug 2019, 18:45 last edited by
                  #8

                  @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

                  J 1 Reply Last reply 8 Aug 2019, 18:53
                  1
                  • R raven-worx
                    8 Aug 2019, 18:45

                    @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.

                    J Offline
                    J Offline
                    JonB
                    wrote on 8 Aug 2019, 18:53 last edited by
                    #9

                    @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?

                    R 1 Reply Last reply 8 Aug 2019, 19:23
                    0
                    • J JonB
                      8 Aug 2019, 18:53

                      @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?

                      R Offline
                      R Offline
                      raven-worx
                      Moderators
                      wrote on 8 Aug 2019, 19:23 last edited by
                      #10

                      @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
                      2
                      • R Offline
                        R Offline
                        Robert Hairgrove
                        wrote on 10 Aug 2019, 14:39 last edited by Robert Hairgrove 8 Oct 2019, 14:41
                        #11

                        @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
                        0
                        • R Offline
                          R Offline
                          Robert Hairgrove
                          wrote on 10 Aug 2019, 17:21 last edited by
                          #12

                          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?

                          R 1 Reply Last reply 10 Aug 2019, 18:12
                          0
                          • R Robert Hairgrove
                            10 Aug 2019, 17:21

                            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?

                            R Offline
                            R Offline
                            raven-worx
                            Moderators
                            wrote on 10 Aug 2019, 18:12 last edited by
                            #13

                            @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 10 Aug 2019, 18:26
                            2
                            • R raven-worx
                              10 Aug 2019, 18:12

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

                              the model handles everything itself?

                              yes

                              R Offline
                              R Offline
                              Robert Hairgrove
                              wrote on 10 Aug 2019, 18:26 last edited by
                              #14

                              @raven-worx : Thanks very much!

                              1 Reply Last reply
                              0

                              1/14

                              8 Aug 2019, 15:25

                              • Login

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