PDA

View Full Version : QTableWidget: set items disappear after new insertion



Raccoon29
16th March 2008, 16:49
I have a QTableWidget which has to display a sql result, tipical situation.
The problem is that in a for cycle, when a row is inserted (composed by 5 items), previouse inserted rows disappear, so the cycle lasts with the table that contains just the last record and the previouse ones are blank... this happen even if I followed the "stardelegate" example...

Any idea about why it is behaving in such a way?

This is the incriminated insertion piece:

(ui.tbwview is the QTableWidget)



[...]
ui.tbwview->setRowCount(0);
ui.tbwview->setRowCount(length);
int i=0;
while(query.next())
{
QTableWidgetItem *id=new QTableWidgetItem(query.value(0).toString());
QTableWidgetItem *code=new QTableWidgetItem(query.value(1).toString());
QTableWidgetItem *description=new QTableWidgetItem(query.value(2).toString());
QTableWidgetItem *um=new QTableWidgetItem(query.value(3).toString());
QTableWidgetItem *gm=new QTableWidgetItem(query.value(4).toString());

ui.tbwview->setItem(i,0,id);
ui.tbwview->setItem(i,1,code);
ui.tbwview->setItem(i,2,description);
ui.tbwview->setItem(i,3,um);
ui.tbwview->setItem(i,4,gm);
i++;
}

wysota
16th March 2008, 17:35
What does "length" contain? setItem() won't create new rows, it'll substitute the ones already existing, are you aware of that?

Raccoon29
16th March 2008, 17:51
What does "length" contain? setItem() won't create new rows, it'll substitute the ones already existing, are you aware of that?
Yes, I am.
length (sorry, I forgot to explain it) is an int valorized by the query "SELECT COUNT(*) FROM table" and it contains the number of records fetched by the query.
In substance, I'm trying to create all the required rows and then populate them with setItem(). Isn't it expected to work in this way too? Is there a better way?

wysota
16th March 2008, 18:56
Well... using QSqlQueryModel or QSqlTableModel together with QTableView would be more straightforward.

Raccoon29
16th March 2008, 19:07
Well... using QSqlQueryModel or QSqlTableModel together with QTableView would be more straightforward.
Hehe yeah, in fact these seem much easier and usual than my solution...

anyway I solved it: the QTableWidget had sortingEnabled to true and this somehow messed up every item each time I inserted them... I disabled sorting and now all works as it should.

Thank you anyway for your help, maybe all this could avoid headaches to someone else :)

wysota
16th March 2008, 19:18
anyway I solved it: the QTableWidget had sortingEnabled to true and this somehow messed up every item each time I inserted them... I disabled sorting and now all works as it should.

See the docs for QTableWidget::setItem:

If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

Raccoon29
16th March 2008, 19:28
See the docs for QTableWidget::setItem:
Funny: I found it just after I posted here the solution... you can imagine how many laughs...and head hits on the wall...