How to remove multiple rows from QTable Widget?

混江龙づ霸主 提交于 2021-02-18 15:00:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!