PDA

View Full Version : adding items in qtablewidget



Djony
22nd November 2006, 20:40
Hi, I am having problems to show cells in my qtablewidget. These are parts of code that I use to implement qtablewidgetitems:


tableTestSuits->setRowCount(0); // tableTestSuits is QTablewidget *

for (unsigned j=0; j<vTestCaseResults.size();j++)
{
.......
QTableWidgetItem *item=new QTableWidgetItem (tr(tempName.toLatin1()));
QTableWidgetItem *item3=new QTableWidgetItem (tr(tempVersion.toLatin1()));
QTableWidgetItem *item5=new QTableWidgetItem (tr(tempResult.toLatin1()));
QTableWidgetItem *item2=new QTableWidgetItem (tr(tempDate.toLatin1()));
QTableWidgetItem *item4=new QTableWidgetItem (tr(tempDuration.toLatin1()));
tableTestSuits->insertRow(j);
tableTestSuits->setItem(j,0,item2);
tableTestSuits->setItem(j,3,item4);
tableTestSuits->setItem(j,4,item5);
tableTestSuits->setItem(j,1,item3);
tableTestSuits->setItem(j,2,item);
}

I am pretty sure that strings for qtablewidgetitems are ok. So, there must be something else wrong, but I don't know what. Any suggestions welcomed.

jacek
22nd November 2006, 21:03
What value did you set the columnCount to?

Djony
23rd November 2006, 13:57
I set it to 5

jacek
23rd November 2006, 14:10
OK, do you see row and column labels on the table headers?

Djony
23rd November 2006, 14:14
I see solumn headers, yes, but row headers no, because I've hide it with verticalHeader()->hide() method.

jacek
23rd November 2006, 14:36
I see solumn headers, yes, but row headers no, because I've hide it with verticalHeader()->hide() method.
Can you see them, if you comment out that line?

Djony
23rd November 2006, 14:47
yes, I can see them.

jpn
23rd November 2006, 15:03
I'd still suspect the problem is with those strings. What happens if you replace:

QTableWidgetItem *item=new QTableWidgetItem (tr(tempName.toLatin1()));
with something like

QTableWidgetItem *item=new QTableWidgetItem ("blaa");
?

Djony
23rd November 2006, 15:11
I tried your stuff and it doesn't work, but found out very odd thing.

When I replace j with zero (that means this code):

tableTestSuits->insertRow(0);

tableTestSuits->setItem(0,0,item2);
tableTestSuits->setItem(0,3,item4);
tableTestSuits->setItem(0,4,item5);
tableTestSuits->setItem(0,1,item3);
tableTestSuits->setItem(0,2,item);
-----------------------------------------------

I get cell properly. I found this very odd. Have any ideas about it?

jacek
23rd November 2006, 16:09
When I replace j with zero [...] I get cell properly. I found this very odd. Have any ideas about it?
What is the value of "j" just before the insertRow() line?

Djony
23rd November 2006, 16:15
j is parameter of for loop. Look my first post on this topic

jacek
23rd November 2006, 16:19
j is parameter of for loop. Look my first post on this topic
I see it, but there's also "......." in the beginning of the loop body --- maybe you do something with it there?

Djony
23rd November 2006, 16:21
No, I don't do anything with it. Just use for indexing. Still, have no idea. Very odd...

jacek
23rd November 2006, 19:17
Does this work on your system?
#include <QApplication>
#include <QTableWidget>

const int nRows = 10;
const int nCols = 10;

int main( int argc, char **argv )
{
QApplication app( argc, argv );

QTableWidget tw;
tw.setColumnCount( nCols );

for( int i = 0; i < nRows; ++i ) {
tw.insertRow( i );
for( int j = 0; j < nCols; ++j ) {
QTableWidgetItem *item = new QTableWidgetItem( QString("(%1,%2)").arg(i).arg(j));
tw.setItem( i, j, item );
}
}

tw.show();

return app.exec();
}

Djony
23rd November 2006, 19:38
Yes, it runs OK. Funny, though, I've noticed some other things about my code for qtablewidget. For some iterations, qtablewidgets prints ok, and for some input not. I understand that strings are most suspicious, but, i've printed them with cout in console, and they are OK, just as they should be. Plus, if I just replace j with zero, code runs well. If I changed just index of rows, then strings aren't problem. Am I wright?

jacek
23rd November 2006, 20:04
but, i've printed them with cout in console, and they are OK,
What did you try to print exactly? "tr(tempName.toLatin1())" or only "tempName"?

Djony
24th November 2006, 08:28
i've printed tempName.toStdString()

jacek
24th November 2006, 10:03
Then try printing tr(tempName.toLatin1()).toStdString(). Maybe it gets translated to an empty string?