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. Custom QTableView and model: InternalMove also causes insert/delete

Custom QTableView and model: InternalMove also causes insert/delete

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 906 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.
  • J Offline
    J Offline
    JohnGa
    wrote on last edited by
    #1

    Hello,
    I have a custom QTableView and a custom model derived from QAbstractTableModel. I only want to allow for rows to be dragged and dropped within the tableview (basically only re-ordering of the rows is allowed).

    I am applying the following settings:

        qTableView->setDropIndicatorShown(true);
        qTableView->setDragEnabled(true);
        qTableView->viewport()->setAcceptDrops(true);
        qTableView->setDragDropMode(QAbstractItemView::InternalMove);
        qTableView->setDefaultDropAction(Qt::MoveAction);
        qTableView->setDragDropOverwriteMode(false);
    

    I also have a custom Row object with 10 attributes, and only 5 of them are shown in the tableview.

    When I drag/drop, I see that a new row is inserted at the new location, the attributes visible in tableview are copied over to the new row, and the row at the old location is deleted.

    How can I copy the 5 attributes from the row at the old location to the row at the new location? Is there a way to move the row, instead of insert/delete?

    Thanks.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QAbstractItemView::startDrag() is only using the selectedIndexes (QAbstractItemViewPrivate::selectedDraggableIndexes()) so I would suggest to reimplement QAbstractItemModel::mimeData() and return all items, the same then for QAbstractItemModel::setMimeDatta().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • J Offline
        J Offline
        JohnGa
        wrote on last edited by JohnGa
        #3

        @Christian-Ehrlicher said in Custom QTableView and model: InternalMove also causes insert/delete:

        QAbstractItemView

        Christian,
        Thanks for taking the time to reply. Let me try and explain my situation again.

        I have a custom row object with 10 attributes. Only 5 of the attributes are displayed in the tableview. The other 5 attributes are private data which I need for database identity, etc.

        I want to drag/drop row only within the table. Basically I want to move the row from one index in the model to another index.

        Qt by default does not seem to support row moves. By default, it inserts a new row at the new index, copies the 5 attributes that are visible in the tableview, and then deletes the old row at the old index. The other 5 attributes in the row that are NOT displayed in the tableview, are NOT copied over to the new row.

        How do I copy the 5 other attributes from the row at the old location, to the row at the new location before the old row is deleted? Ideally, I would like to move the row. But looks like even InternalMove does an insert/copy/delete. If the copy

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JohnGa said in Custom QTableView and model: InternalMove also causes insert/delete:

          How do I copy the 5 other attributes from the row at the old location, t

          I already explained how to do so - you have to reimplement mimeData() / setMimeData().

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • J Offline
            J Offline
            JohnGa
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            Now I get it. The data is transferred from the old row to the new row via the mimeData and setMimeData. By overriding, instead of it picking the visibile table columns/properties I can send all the attributes.

            Thanks.

            J 1 Reply Last reply
            0
            • J JohnGa

              @Christian-Ehrlicher
              Now I get it. The data is transferred from the old row to the new row via the mimeData and setMimeData. By overriding, instead of it picking the visibile table columns/properties I can send all the attributes.

              Thanks.

              J Offline
              J Offline
              JohnGa
              wrote on last edited by JohnGa
              #6

              I marked the wrong message as the correct answer by mistake, and it won't let me change it. The first reply by @Christian-Ehrlicher is the correct answer. Full credit to @Christian-Ehrlicher

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

                Hi,

                It has been fixed.

                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

                • Login

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