QTableWidget 删除单行 或者 多行

一、只删除一行

    int rowIndex = tableWidget->currentRow();
    if (rowIndex != -1){
        tableWidget->removeRow(rowIndex);
}

二、删除一行 或者 多行均能使用

   首先需要获得选区,可以用 QItemSelectionModel 和 QModelIndexList 实现

 1     QMessageBox *msg = new QMessageBox(tableWidget);
 2     msg->question(tableWidget, "warning", "Delete data can't restore, please confirm.", QMessageBox::Yes, QMessageBox::No);
 3     if (QMessageBox::Yes)
 4     {
 5         QItemSelectionModel  *selection = tableWidget->selectionModel();
 6         QModelIndexList indexList = selection->selectedIndexes();
 7 
 8         QMap<int, int> rowMap;
 9         foreach(QModelIndex index, indexList)
10         {
11             rowMap.insert(index.row(), 0);
12         }
13 
14         QMapIterator<int, int> rowMapIterator(rowMap);
15         rowMapIterator.toBack();
16         while (rowMapIterator.hasPrevious())
17         {
18             rowMapIterator.previous();
19             int rowToDel = rowMapIterator.key();
20             tableWidget->removeRow(rowToDel);
21             TABLE->deleteSample(rowToDel);
22         }
23     }

 

原文地址:https://www.cnblogs.com/jiangson/p/6908265.html