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. QListWidget - Keep track of moved rows

QListWidget - Keep track of moved rows

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 503 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I have a class that inherits from QListWidget that has method connected to QAbstractItemModel::rowsMoved signal.

    When I move the rows I need to track their new positions.

    void PlaylistPage::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int row)
    {
            
    }
    

    Let's say the table has 9 rows and I move the rows 1,2,3 to position 6 and I want to know where the row in position 2 will be when rows are dropped. The rows are moved through drag and drop.

    JonBJ 1 Reply Last reply
    0
    • H hbatalha

      I have a class that inherits from QListWidget that has method connected to QAbstractItemModel::rowsMoved signal.

      When I move the rows I need to track their new positions.

      void PlaylistPage::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int row)
      {
              
      }
      

      Let's say the table has 9 rows and I move the rows 1,2,3 to position 6 and I want to know where the row in position 2 will be when rows are dropped. The rows are moved through drag and drop.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @hbatalha
      So 2 will end up being new 5?

      H 1 Reply Last reply
      0
      • JonBJ JonB

        @hbatalha
        So 2 will end up being new 5?

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

        @JonB it will be 4.

        JonBJ 1 Reply Last reply
        0
        • H hbatalha

          @JonB it will be 4.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @hbatalha
          Upon reflection you are quite right! I was mentally placing them after 6, but of course they get inserted at 6 to come before:

          4
          5
          1
          2
          3
          6
          ...
          
          H 1 Reply Last reply
          0
          • JonBJ JonB

            @hbatalha
            Upon reflection you are quite right! I was mentally placing them after 6, but of course they get inserted at 6 to come before:

            4
            5
            1
            2
            3
            6
            ...
            
            H Offline
            H Offline
            hbatalha
            wrote on last edited by hbatalha
            #5

            @JonB So I came up with a solution. I use a variable to store the difference between the position of the row I want to keep track and the last row position out of the selected rows.

            //mRowsDifference - holds the position of the row I want to keep track of
            
            void PlaylistPage::onRowsMoved(const QModelIndex &, int start, int end, const QModelIndex &, int row)
            {    
                 mCurrentPlayingIndex = this->selectedIndexes().last().row() - mRowsDifference;  // after the last row is moved mCurrentPlayingIndex will be holding the new position
            }
            
            void PlaylistPage::dragMoveEvent(QDragMoveEvent *event)
            {
                 mRowsDifference = this->selectedIndexes().last().row() - mCurrentPlayingIndex;
                    
                 QListWidget::dragMoveEvent(event);
            }
            

            I will be making more tests but until it seems to be working

            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