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 drag & drop rows within QTableWidget
QtWS25 Last Chance

How to drag & drop rows within QTableWidget

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 22.5k 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
    jarda
    wrote on last edited by
    #1

    Hello all,
    I want to drag & drop rows in QTableWidget to change order of rows in the table.
    I want only move whole rows, not overwrite or delete.

    I've tried
    @
    table->setDragDropOverwriteMode(false);
    table->setDragEnabled(true);
    table->setDragDropMode(QAbstractItemView::InternalMove);
    item->setFlags(item->flags() & ~(Qt::ItemIsDropEnabled));
    @
    but it doesn't fully enable to move a row.

    My question is: is it possible to get requested behavior just by correct setting of some flags or is it necessary to inherit and re-implement something?
    Working example is appreciated.

    Thanks in advance,

    Jarda

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sigrid
      wrote on last edited by
      #2

      The example below allows you to move rows by calling "setMovable(true)":http://doc.qt.nokia.com/4.7/qheaderview.html#setMovable on the header. Does it give you the behavior you want?

      @
      #include <QtGui>

      int main( int argc, char** argv ) {
      QApplication a( argc, argv );
      QTableWidget table(4,4);
      for(int row =0;row<4;row++)
      for(int col = 0;col<4;col++)
      {
      QTableWidgetItem *item = new QTableWidgetItem(QString("Row%1 Col%2").arg(row).arg(col));
      item->setFlags(item->flags() | Qt::ItemIsSelectable);
      table.setItem(row,col,item);
      }
      table.verticalHeader()->setMovable(true);
      table.show();
      return a.exec();
      }
      @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jarda
        wrote on last edited by
        #3

        Thank you for reply. but I also need to change the number of moved row. When I drag the row number 5 to first row I also need it to become row number 1 and reorder the rest (not only the row number 5 displayed as first).

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          I'm not sure if that is possible within QTableWidget. If you do it with QTableView and a custom model, you would overwrite the drag / drop stuff in the model.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jarda
            wrote on last edited by
            #5

            Thank You Gerolf for reply. Can you please give me a hint (or sample code) how to implement it with custom model?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              If you look inside the "qt docs":http://doc.qt.nokia.com/4.7/model-view-programming.html there ios a chapter on model view and also on dnd with MVD. I propose you start reading there.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gadlol
                wrote on last edited by
                #7

                I wanted the same functionality for my app. So I searched through internet and didnt find anything. So I had to do it.

                So, you can do it with QTableWidget by using the cellEntered(int,int); then takeItem(row, col) and then setItem()...

                @connect(ui->tableWidget, SIGNAL(cellEntered(int,int)), this, SLOT(cellEnteredSlot(int,int)));@

                @void MainWindow::cellEnteredSlot(int row, int column){

                    int colCount=ui->tableWidget->columnCount();
                
                    int rowsel;
                    
                    if(ui->tableWidget->currentIndex().row()<row) rowsel=row-1; //down
                
                    else if(ui->tableWidget->currentIndex().row()>row) rowsel=row+1; //up
                
                    else return;
                
                            QList<QTableWidgetItem*> rowItems,rowItems1;
                            for (int col = 0; col < colCount; ++col)
                            {
                                    rowItems << ui->tableWidget->takeItem(row, col);
                                    rowItems1 << ui->tableWidget->takeItem(rowsel, col);
                
                            }
                
                            for (int cola = 0; cola < colCount; ++cola)
                            {
                                    ui->tableWidget->setItem(rowsel, cola, rowItems.at(cola));
                                    ui->tableWidget->setItem(row, cola, rowItems1.at(cola));
                
                            }
                

                }@

                Warning: I think theres a bug.

                Although it works fine by clicking and holding the left mouse button and moving mouse up or down, it also works with Mouse WHEEL Scroll.

                I have not figure it out yet on how to check if mouse wheel was triggered so not to move selected row.

                The doc says:

                bq. This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.

                BUT when you scroll the mouse wheel is not a mouse button press action.

                So I think its a BUG. Should I submit it?

                hello programming

                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