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. QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 687 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1

    Env

    • Qt 6.2.1
    • Linux
    • KDE

    Problem

    The problem only occurs when move rows in the same parent, only one row moving is tested
    e.g. in QTreeView

    1. 5 rows in one parent
    2. when moving index 0 to index 2 (.i.e. before index 2), it will exchange index 0 and 1, it should call beginMoveRows(parent, 0, 0, parent, 2). If calling beginMoveRows(parent, 0, 0, parent, 1), endMoveRows fails.
    3. when moving index 0 to the last (.i.e after index 5), if following the rule of step 2, it should call beginMoveRows(parent, 0, 0, parent, 5), but rowsMoved signal handler fails, because index 5 is out of range and model()->index(row, 0, destination) returns an invalid model index

    Question

    When moving a row in the same parent, what's the proper way to make it work for both

    • moving a row one row down
    • moving a row to the last

    Is the way in step 2 right?
    If it is right, row for destination should be adjusted manually in rowsMoved signal handler when moving a row to the last in the same parent?

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

      @jronald said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:

      which is out of range, is it as expected?

      Yes, it's correct. If the start is before the end and you want to know where it is now you have to do some basic math.
      rowsMoved() simply informs you about what was given to beginMoveRows()

      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
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        QAbstractItemModel is correct, please provide a minimal, compilable example.

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

        jronaldJ 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          QAbstractItemModel is correct, please provide a minimal, compilable example.

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by jronald
          #3

          @Christian-Ehrlicher said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:

          QAbstractItemModel is correct, please provide a minimal, compilable example.

          It takes a lot time to write a sample for this.
          To simplify the question,
          e.g.

          1. In QTreeView, within one parent, there are 5 items
          2. Drag and drop the 1st item to the last in the same parent
          3. In rowsMoved signal handler as below, param row is 5, which is out of range, is it as expected?
            void QAbstractItemModel::rowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
            
          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @jronald said in QAbstractItemModel::rowsMoved problem when moving a row to the last in the same parent:

            which is out of range, is it as expected?

            Yes, it's correct. If the start is before the end and you want to know where it is now you have to do some basic math.
            rowsMoved() simply informs you about what was given to beginMoveRows()

            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
            3

            • Login

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