C++ 如何从QTableWidget中删除所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15848086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all rows from QTableWidget
提问by vinayan
I am trying to delete all rows from a QTableWidget. Here is what I tried.
我正在尝试从QTableWidget 中删除所有行。这是我尝试过的。
for ( int i = 0; i < mTestTable->rowCount(); ++i )
{
mTestTable->removeRow(i);
}
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,
我的桌子上有两行。但这只是删除了一行。一个原因可能是我没有创建具有固定表大小的表。rowCount()的 Qt 文档说,
This property holds the number of rows in the table.
By default, for a table constructed without row and column counts, this property contains a value of 0.
此属性保存表中的行数。
默认情况下,对于没有行数和列数构造的表,此属性包含值 0。
So if that is the case, what is the best way to remove all rows from table?
因此,如果是这种情况,从表中删除所有行的最佳方法是什么?
回答by alexisdm
Just set the row count to 0 with:
只需使用以下命令将行数设置为 0:
mTestTable->setRowCount(0);
it will delete the QTableWidgetItem
s automatically, by calling removeRows
as you can see in QTableWidget
internal model code:
它将QTableWidgetItem
自动删除s,通过调用removeRows
,您可以在QTableWidget
内部模型代码中看到:
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);
}
回答by john
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.
我不知道,QTableWidget
但您的代码似乎存在逻辑缺陷。您忘记了在循环时您正在减少 的值mTestTable->rowCount()
。删除一行后,i
将是一,mTestTable->rowCount()
也将是一,因此您的循环停止。
I would do it like this
我会这样做
while (mTestTable->rowCount() > 0)
{
mTestTable->removeRow(0);
}
回答by damkrat
AFAIK setRowCount(0)
removes nothing. Objects are still there, but no more visible.
AFAIKsetRowCount(0)
不会删除任何内容。对象仍然存在,但不再可见。
yourtable->model()->removeRows(0, yourtable->rowCount());
回答by Alex Gurkin
QTableWidget test;
test.clear();
test.setRowCount( 0);
回答by Jens A. Koch
The simple way to delete rows is to set the row count to zero. This uses removeRows() internally.
删除行的简单方法是将行数设置为零。这在内部使用 removeRows()。
table->setRowCount(0);
You could also clear the content and then remove all rows.
您还可以清除内容,然后删除所有行。
table->clearContents();
table->model()->removeRows(0, table->rowCount());
Both snippets leave the headers untouched!
两个片段都保持标题不变!
If you need to get rid of headers, too, you could switch from clearContents()
to clear()
.
如果您也需要摆脱标题,您可以从 切换clearContents()
到clear()
。
回答by Rémi Debord
In order to prevent an app crash, disconnect all signals from the QTableView.
为了防止应用程序崩溃,请断开 QTableView 的所有信号。
// Deselects all selected items
ui->tableWidget->clearSelection();
// Disconnect all signals from table widget ! important !
ui->tableWidget->disconnect();
// Remove all items
ui->tableWidget->clearContents();
// Set row count to 0 (remove rows)
ui->tableWidget->setRowCount(0);
回答by Alex
You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):
您可以将空项目模型 (QStandardItemModel) 添加到您的 QTableView (myTableView):
itemModel = new QStandardItemModel;
ui->myTableView->setModel(itemModel);
回答by user2076767
Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.
从视图中删除不在标题中的所有项目。这也将删除所有选择。桌子尺寸保持不变。
void QTableWidget::clearContents()
Removes all items in the view. This will also remove all selections and headers.
删除视图中的所有项目。这也将删除所有选择和标题。
void QTableWidget::clear()
回答by AB Bolim
Your code does not delete last row.
您的代码不会删除最后一行。
Try this one.
试试这个。
int totalRow = mTestTable->rowCount();
for ( int i = 0; i < totalRow ; ++i )
{
mTestTable->removeRow(i);
}
In your code, on the first time, rowCount()
have value 2
and value of the i
is 0
, so its delete 1
st row,
在您的代码中,第一次rowCount()
具有is 的值2
和值,因此它删除st 行,i
0
1
But on the second time value of i
incremented with 1
, but rowCount()
return the updated row count which is now 1, so, it does not delete the last row.
但是在第二次以i
递增的值时1
,rowCount()
返回更新后的行数,现在为 1,因此,它不会删除最后一行。
Hope now you ll be clear.
希望现在你会清楚。
回答by d.danailov
Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row
看看这个帖子: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));
}