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?
如果是这样,从表中删除所有行的最佳方法是什么?
10 个解决方案
#1
62
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:
它将通过调用removeRows自动删除QTableWidgetItems,如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);
}
#2
28
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将是1,mTestTable->rowCount()也将是1,因此循环将停止。
I would do it like this
我应该这样做
while (mTestTable->rowCount() > 0)
{
mTestTable->removeRow(0);
}
#3
9
AFAIK setRowCount(0)
removes nothing. Objects are still there, but no more visible.
AFAIK setRowCount(0)删除。物体仍然存在,但不再可见。
yourtable->model()->removeRows(0, yourtable->rowCount());
#4
5
QTableWidget test;
test.clear();
test.setRowCount( 0);
#5
3
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()
.
如果需要删除header,可以从clearContents()切换到clear()。
#6
2
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);
#7
0
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()的值为2,i的值为0,所以第一行删除,
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.
但是,在第二次的值中,我增加了1,但是rowCount()返回了更新后的行数,现在是1,因此,它不会删除最后一行。
Hope now you ll be clear.
希望你现在明白了。
#8
0
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()
#9
0
Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row
看看这篇文章:http://forum.qt.io/topic/1715/qtablewidget- 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));
}
#10
0
You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):
您可以将空项目模型(qstandardtemmodel)添加到QTableView (myTableView):
itemModel = new QStandardItemModel;
ui->myTableView->setModel(itemModel);
#1
62
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:
它将通过调用removeRows自动删除QTableWidgetItems,如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);
}
#2
28
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将是1,mTestTable->rowCount()也将是1,因此循环将停止。
I would do it like this
我应该这样做
while (mTestTable->rowCount() > 0)
{
mTestTable->removeRow(0);
}
#3
9
AFAIK setRowCount(0)
removes nothing. Objects are still there, but no more visible.
AFAIK setRowCount(0)删除。物体仍然存在,但不再可见。
yourtable->model()->removeRows(0, yourtable->rowCount());
#4
5
QTableWidget test;
test.clear();
test.setRowCount( 0);
#5
3
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()
.
如果需要删除header,可以从clearContents()切换到clear()。
#6
2
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);
#7
0
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()的值为2,i的值为0,所以第一行删除,
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.
但是,在第二次的值中,我增加了1,但是rowCount()返回了更新后的行数,现在是1,因此,它不会删除最后一行。
Hope now you ll be clear.
希望你现在明白了。
#8
0
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()
#9
0
Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row
看看这篇文章:http://forum.qt.io/topic/1715/qtablewidget- 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));
}
#10
0
You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):
您可以将空项目模型(qstandardtemmodel)添加到QTableView (myTableView):
itemModel = new QStandardItemModel;
ui->myTableView->setModel(itemModel);