问题
I want to remove multiple rows which are selected in the QTableWidget . can anyone have any ideas ?
回答1:
You can remove multiple items at once, The tips, is begin delete the rows from the bottom. i'm sorry im not a VS2008 developer, but this solution can be easy convert to VS2008 code. Here the Python code. Sorry for the late response :)
# Delete the selected mytable lines
deleteRows(self.mytable.selectionModel().selectedRows())
# DeleteRows function
def deleteRows(self, rows):
# Get all row index
indexes = []
for row in rows:
indexes.append(row.row())
# Reverse sort rows indexes
indexes = sorted(indexes, reverse=True)
# Delete rows
for rowidx in indexes:
self.mytable.removeRow(rowidx)
回答2:
if you want to remove some rows at the end of the table, you can try using setRowCount(int row).
i was looking for a way to remove multiple selected rows from different places in the table some time back too.
i ended up iterating through my selected list from the bottom to delete my table.
回答3:
Maybe this can help :
QList<QTableWidgetItem*> selected_itms = ur_table->selectedItems();
while( !selected_itms.isEmpty() )
{
QTableWidgetItem *itm = selected_itms.at(0);
ur_table->removeRow(itm->row());
selected_itms = ui.tblw_entries->selectedItems();
}
回答4:
QList<QTableWidgetItem*> itemList = widget->selectedItems();
for(int i = 0; i < itemList.size(); i++) {
widget->removeCellWidget(itemList.at(i)->row(), itemList.at(i)->column());
}
来源:https://stackoverflow.com/questions/8907511/how-to-remove-multiple-rows-from-qtable-widget