How to delete all rows from QTableWidget

前端 未结 10 1875
自闭症患者
自闭症患者 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:17

    Just set the row count to 0 with:

    mTestTable->setRowCount(0);
    

    it will delete the QTableWidgetItems automatically, by calling removeRows as you can see in QTableWidget internal model code:

    void QTableModel::setRowCount(int rows)
    {
        int rc = verticalHeaderItems.count();
        if (rows < 0 || rc == rows)
            return;
        if (rc < rows)
            insertRows(qMax(rc, 0), rows - rc);
        else
            removeRows(qMax(rows, 0), rc - rows);
    }
    

提交回复
热议问题