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. How to update displayed row number after dragdrop in QTableView
Forum Updated to NodeBB v4.3 + New Features

How to update displayed row number after dragdrop in QTableView

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

    Hello,
    I successfully enabled the ability to move rows within a QTableView. I also have the row numbers display (In the row headers) enabled. The following are my settings:
    ```
    qTableView->verticalHeader()->setSectionsMovable(true);
    qTableView->verticalHeader()->setDragEnabled(true);
    qTableView->verticalHeader()->setDragDropMode(QAbstractItemView::InternalMove);

    Let's say I start out with rows (row contents in braces): 
    Row #1 -> (A), Row #2  -> (B), Row #3 -> (C).
    
    I grab and move row 1, to after row 3.
    
    The view now shows the following:
    Row #2  -> (B), Row #3 -> (C), Row #1 -> (A),
    
    How can I update and reassign the row numbers in the view?  After the item has been dropped I want it to show:
    Row #1 -> (B), Row #2  -> (C), Row #3 -> (A)
    
    Thanks.
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What model do you have underneath ?

      In any case, you likely want to re-implement the model's headerData method.

      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
      • J Offline
        J Offline
        JohnGa
        wrote on last edited by
        #3

        I have my own implementation of the model. It is subclassed off of QAbstractTableModel. I have already re-implemented the headerData method.

        In the headerData method, I currently have the following code to display the row numbers:

            if(orientation == Qt::Vertical)
            {
                return QString::number(section + 1);
            }
        

        Can you please give me a hint on what I need to do in the headerData method?

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

          Ok... It's a bit more tricky than that as it seems.

          Use this custom QHeaderView class:

          class HeaderView : public QHeaderView
          {
          public:
          
              HeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
              : QHeaderView(orientation, parent)
              {}
          
          
          void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
          {
              QHeaderView::paintSection(painter, rect, visualIndex(logicalIndex));
          }
          
          };
          
          

          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
          1
          • J Offline
            J Offline
            JohnGa
            wrote on last edited by
            #5

            SGaist,
            You are truly amazing. I was able to get it to work with this.

            One last tip request: After the drop has finished I want to take the moved row, access it's details and do further processing. How can I accomplish this?

            Thanks.

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

              You can re-implement dropMimeData and launch the processing from there.

              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
              1

              • Login

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