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?
QtWS25 Last Chance

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.8k Views
  • 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

    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?

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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

    JonBJ 1 Reply Last reply
    2
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      • raven-worxR raven-worx

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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.

        raven-worxR 1 Reply Last reply
        0
        • JonBJ 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-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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

          JonBJ 1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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?

            raven-worxR 1 Reply Last reply
            0
            • JonBJ 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-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on 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

              JonBJ 1 Reply Last reply
              1
              • raven-worxR raven-worx

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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?

                raven-worxR 1 Reply Last reply
                0
                • JonBJ JonB

                  @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-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on 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 last edited by Robert Hairgrove
                    #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 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?

                      raven-worxR 1 Reply Last reply
                      0
                      • R Robert Hairgrove

                        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-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on 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
                        2
                        • raven-worxR raven-worx

                          @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 last edited by
                          #14

                          @raven-worx : Thanks very much!

                          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