How to delete all rows from QTableWidget

前端 未结 10 1871
自闭症患者
自闭症患者 2021-01-31 14:57

I am trying to delete all rows from a QTableWidget . Here is what I tried.

for ( int i = 0; i < mTestTable->rowCount(); ++i )
{
    mTestTable->removeRo         


        
相关标签:
10条回答
  • 2021-01-31 15:32

    AFAIK setRowCount(0) removes nothing. Objects are still there, but no more visible.

    yourtable->model()->removeRows(0, yourtable->rowCount());
    
    0 讨论(0)
  • 2021-01-31 15:35

    Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row

    QList<QTableWidgetItem*> items = table.findItems(.....);
    QMap<int, int> rowsMap;
    for(int i = 0; i < items.count(); i++{
      rowsMap[items.at(i).row()] = -1; //garbage value
    }
    QList<int> rowsList = rowsMap.uniqueKeys();
    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));
    }
    
    0 讨论(0)
  • 2021-01-31 15:38

    I don't know QTableWidget but your code seems to have a logic flaw. You are forgetting that as you go round the loop you are decreasing the value of mTestTable->rowCount(). After you have removed one row, i will be one and mTestTable->rowCount() will also be one, so your loop stops.

    I would do it like this

    while (mTestTable->rowCount() > 0)
    {
        mTestTable->removeRow(0);
    }
    
    0 讨论(0)
  • 2021-01-31 15:41

    You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):

    itemModel = new QStandardItemModel;
    ui->myTableView->setModel(itemModel);
    
    0 讨论(0)
提交回复
热议问题