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. QTableWidget - how to delete a row?

QTableWidget - how to delete a row?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 63.4k 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.
  • M Offline
    M Offline
    micc
    wrote on last edited by
    #1

    What is the best way to delete a row in a QTableWidget table?
    I'm probably missing something, but all I can see so far is "delete", and that only deletes the contents of a single cell (row,col).

    I want to implement code to find and remove a table row, shifting all the remaining rows up to close the gap.

    I can use findItems to find the relevant cell, but this just returns a QTableWidgetItem, not the row/col.

    Do I need to implement a loop, looking at each row in turn, in order to find the row to be deleted, or is there a better way?

    Thanks.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Hi,

      I guess you should use "removeRow":http://doc.qt.nokia.com/4.7/qtablewidget.html#removeRow

      Tony.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        micc
        wrote on last edited by
        #3

        Thanks Tony.

        I thought there might be a way to avoid the loop, not that it's difficult.
        I was hoping there might be a way of identifying the row to be deleted using the "findItems" utility, rather than having to interrogate each row in turn.

        Anyway, I'll stick with the loop, thanks for your help.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          iunknwn
          wrote on last edited by
          #4

          bq. I can use findItems to find the relevant cell, but this just returns a QTableWidgetItem, not the row/col.

          You can still use findItems.
          findItems would return QList<QTableWidgetItem *>. You can do following:

          @QList<QTableWidgetItem*> items = table.findItems(.....);
          QMap<int, int> rowsMap;
          for(int i = 0; i < items.count(); i++{
          rowsMap[items.at(i).row()] = -1; //garbage value
          }
          QList<int> rowsList = rowsMap.uniqueKeys();
          qSort(rowsList);

          //Now go through your table and delete rows in descending order as content would shift up and hence cannot do it in ascending order with ease.
          for(int i = rowList.count() - 1; i >= 0; i--){
          table.removeRow(rowList.at(i));
          }@

          Vista x64 with Qt 4.8.2 and VS2010

          1 Reply Last reply
          0
          • 3 Offline
            3 Offline
            3lias
            wrote on last edited by
            #5

            I believe that by using the "removeRow" function you can do your job vrey easy.

            By using this form of removeRow

            ui->tablewidget->removeRow(ui->tablewidget->currentRow());

            you can remove the row you have selected and shift ,the cells above this row, up.

            1 Reply Last reply
            1
            • B Offline
              B Offline
              Bittoo
              wrote on last edited by
              #6

              Hi All,
              You can try below code

              @
              void MSSPectumInputDockWidget::handleDeleteSelectedRow()
              {
              QList<QTableWidgetItem*> selectionRangeList = this->ui->MS2SpectrumInputTableWidget->selectedItems();
              int rowIndex;
              QListIterator<QTableWidgetItem*> selectionRangeListIter(selectionRangeList);

              while(selectionRangeListIter.hasNext()){
                  rowIndex = ((int)((QTableWidgetItem*)selectionRangeListIter.next())->row());
                  this->ui->MS2SpectrumInputTableWidget->removeRow(rowIndex);
              }
              //this->update();
              

              }@

              Still under testing :) for different test cases . Enjoy coding......

              Manoj

              Regards,
              Manoj Kumar Panwar,

              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