Problem with QTableWidget.
-
Hi, I'm having trouble with QTableWidget, I ca't understand a lot of things in it.
First of all, whenever I'm using table->setRowCount(0);
RowCount = 0, but after using insertRow(rowCount); it just increases and increases.
Take a look to following code.[code]int rowCount = table->rowCount(); //row count = 0
table->insertRow(rowCount); //row count = 1
table->setRowCount(0); //rowCount should be 0;
rowCount = table->rowCount(); //MUST be 0.
table->insertRow(rowCount); //row count = 2. WHY?[/code]I also tried removeRow(i) (in loop) and then setRowCount(0); but still same result. using Windows 7 x64.
-
Hello!
Wow, I suppose you have been waiting a while for an answer. I have tried this piece of code, check it yourself:
@
#include <QApplication>
#include <QTableWidget>
#include <iostream>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget *table = new QTableWidget(10, 10);
std::cout << "Number of rows: " << table->rowCount() << std::endl;
std::cout << "Number of columns: " << table->columnCount() << std::endl;
table->setRowCount(0);
table->setColumnCount(0);
std::cout << "Number of rows once setRowCount(0): " << table->rowCount() << std::endl;
std::cout << "Number of columns once setColumnCount(0): " << table->columnCount() << std::endl;
table->insertRow(0);
table->insertColumn(0);
std::cout << "Number of rows once insertRow(0): " << table->rowCount() << std::endl;
std::cout << "Number of columns once insertColumn(0): " << table->columnCount() << std::endl;
//Now yor example
//reset
table->setRowCount(0);
table->setColumnCount(0);int rowCount = table->rowCount(); //row count = 0 std::cout << "table->rowCount()" << std::endl; std::cout << "rowCount is: " << rowCount << std::endl; std::cout << "While the number of rows is: " << table->rowCount() << std::endl; table->insertRow(rowCount); //row count = 1 std::cout << "insertRow(rowCount)" << std::endl; std::cout << "rowCount is: " << rowCount << std::endl; std::cout << "While the number of rows is: " << table->rowCount() << std::endl; table->setRowCount(0); //rowCount should be 0; std::cout << "setRowCount(0)" << std::endl; std::cout << "rowCount is: " << rowCount << std::endl; std::cout << "While the number of rows is: " << table->rowCount() << std::endl; rowCount = table->rowCount(); //MUST be 0. std::cout << "table->rowCount()" << std::endl; std::cout << "rowCount is: " << rowCount << std::endl; std::cout << "While the number of rows is: " << table->rowCount() << std::endl; table->insertRow(rowCount); //row count = 2. WHY? std::cout << "insertRow(rowCount)" << std::endl; std::cout << "Finally, rowCount is: " << rowCount << std::endl; std::cout << "While the number of rows, after the insert is: " << table->rowCount() << std::endl;
//Not shown although executed ^_^
table->setRowCount(9); table->show(); return a.exec();
}@
Output is as follows:
Number of rows: 10
Number of columns: 10
Number of rows once setRowCount(0): 0
Number of columns once setColumnCount(0): 0
Number of rows once insertRow(0): 1
Number of columns once insertColumn(0): 1
table->rowCount()
rowCount is: 0
While the number of rows is: 0
insertRow(rowCount)
rowCount is: 0
While the number of rows is: 1
setRowCount(0)
rowCount is: 0
While the number of rows is: 0
table->rowCount()
rowCount is: 0
While the number of rows is: 0
insertRow(rowCount)
Finally, rowCount is: 0
While the number of rows, after the insert is: 1Let me ask you if your are using your own model (your own data type) for the QTableWidgetItem? If this is the case, then you should use "QTableView":http://qt-project.org/doc/qt-4.8/qtableview.html#public-functions for your purposes. So maybe you are only inserting rows with an accumulative effect... Once I tried to add Items to a QComboBox using my own model, and removeItem() didn't have any effect at all, I had to remove the data from the model. Then, automatically, its item was removed from the QComboBox.
Hope this helped!