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?
Thanks.
5 replies
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:
- for(int i = 0; i < items.count(); i++{
- rowsMap[items.at(i).row()] = -1; //garbage value
- }
- 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));
- }
Hi All,
You can try below code
- void MSSPectumInputDockWidget::handleDeleteSelectedRow()
- {
- QList<QTableWidgetItem*> selectionRangeList = this->ui->MS2SpectrumInputTableWidget->selectedItems();
- int rowIndex;
- while(selectionRangeListIter.hasNext()){
- this->ui->MS2SpectrumInputTableWidget->removeRow(rowIndex);
- }
- //this->update();
- }
Still under testing :) for different test cases . Enjoy coding……
Manoj
You must log in to post a reply. Not a member yet? Register here!

