problem filling a QtableWidget
Hi,
I have a problem filling a qtablewidget. Allways fill the first row. I 've checked the code but I dont see any error. Could you help me please? Many thanks and sorry for my english!
Code:
ui->tabla->setColumnCount(5);
while ( query4.next() )
{
//QString group = query4.value( 0 ).toString();
item->setText(query4.value(0).toString());
ui->tabla->setItem(a,0,item);
item2->setText(query4.value(1).toString());
ui->tabla->setItem(a,1,item2);
item3->setText(query4.value(2).toString());
ui->tabla->setItem(a,2,item3);
item4->setText(query4.value(3).toString());
ui->tabla->setItem(a,3,item4);
//QMessageBox::information(this,"",query4.value(3).toString());
item5->setText(query4.value(4).toString());
ui->tabla->setItem(a,4,item5);
a++;
}
Re: problem filling a QtableWidget
Have you tried to increase the amount of rows in the loop i.e. something like:
Code:
:
:
item5->setText(query4.value(4).toString());
ui->tabla->setItem(a,4,item5);
a++;
ui->tabla->setRowCount(a); // or maybe this needs to be first item in the loop ...
}
Re: problem filling a QtableWidget
The problem is, you're adding five items to QTableWidget and on each iteration changing their contents.
You can't add the same items several times. If you run your code in console you'll get something like this:
Quote:
QTableWidget: cannot insert an item that is already owned by another QTableWidget
You should rather do something like this:
Code:
ui->tabla->setColumnCount(5);
while ( query4.next() ) {
for (int i = 0; i < 5; ++i) {
item->setText(query4.value(i).toString());
ui->tabla->setItem(a,i,item);
}
++a;
}
Re: problem filling a QtableWidget
Quote:
Originally Posted by
calhal
The problem is, you're adding five items to
QTableWidget and on each iteration changing their contents.
You can't add the same items several times. If you run your code in console you'll get something like this:
You should rather do something like this:
Code:
ui->tabla->setColumnCount(5);
while ( query4.next() ) {
for (int i = 0; i < 5; ++i) {
item->setText(query4.value(i).toString());
ui->tabla->setItem(a,i,item);
}
++a;
}
Many thanks! It works!