November 12, 2010

micc micc
Lab Rat
4 posts

QTableWidget - how to delete a row?


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?


5 replies

November 12, 2010

Antonio Di Monaco Antonio Di Monaco
Lab Rat
109 posts


I guess you should use removeRow []


November 12, 2010

micc micc
Lab Rat
4 posts

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.

November 12, 2010

iunknwn iunknwn
Lab Rat
33 posts

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:

  1. QList<QTableWidgetItem*> items = table.findItems(.....);
  2. QMap<int, int> rowsMap;
  3. for(int i = 0; i < items.count(); i++{
  4.   rowsMap[] = -1; //garbage value
  5. }
  6. QList<int> rowsList = rowsMap.uniqueKeys();
  7. qSort(rowsList);
  9. //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.
  10. for(int i = rowList.count() - 1; i >= 0; i--){
  11.   table.removeRow(;
  12. }


Vista x64 with Qt 4.8.2 and VS2010

November 15, 2010

3lias 3lias
Lab Rat
6 posts

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

By using this form of removeRow


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

January 17, 2013

Bittoo Bittoo
Lab Rat
13 posts

Hi All,
You can try below code

  1. void MSSPectumInputDockWidget::handleDeleteSelectedRow()
  2. {
  3.     QList<QTableWidgetItem*> selectionRangeList = this->ui->MS2SpectrumInputTableWidget->selectedItems();
  4.     int rowIndex;
  5.     QListIterator<QTableWidgetItem*> selectionRangeListIter(selectionRangeList);
  7.     while(selectionRangeListIter.hasNext()){
  8.         rowIndex = ((int)((QTableWidgetItem*)>row());
  9.         this->ui->MS2SpectrumInputTableWidget->removeRow(rowIndex);
  10.     }
  11.     //this->update();
  12. }

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



Manoj Kumar Panwar,
Pradeep Research Group
IIT Madras, India
Mobile: +91-9940268725
Email: .(JavaScript must be enabled to view this email address)

  ‹‹ QGraphicsScene with QGLWidget viewport is still active after closing window      Using a user-built lib in application opens also a cmd window. Why? ››

You must log in to post a reply. Not a member yet? Register here!