如何从QTableWidget删除所有行 [英] How to delete all rows from QTableWidget

查看:138
本文介绍了如何从QTableWidget删除所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 QTableWidget 中删除所有行.这是我尝试过的.

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

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

我的桌子上有两行.但这只是删除了一行.原因可能是我没有创建具有固定表大小的表. rowCount()的Qt文档说,

I had two rows in my table. But this just deleted a single row. A reason could be that I did not create the the table with a fixed table size. The Qt Documentation for rowCount() says,

此属性保存表中的行数.

This property holds the number of rows in the table.

默认情况下,对于没有行数和列数的表,该属性的值为0.

By default, for a table constructed without row and column counts, this property contains a value of 0.

如果是这种情况,从表中删除所有行的最佳方法是什么?

So if that is the case, what is the best way to remove all rows from table?

推荐答案

只需使用以下命令将行计数设置为0:

Just set the row count to 0 with:

mTestTable->setRowCount(0);

它会通过调用 removeRows 来自动删除 QTableWidgetItem ,就像您在 QTableWidget 内部模型代码中看到的那样:

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);
}

这篇关于如何从QTableWidget删除所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆