PDA

View Full Version : problem filling a QtableWidget



mmm286
26th May 2010, 10:13
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!



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++;
}

tsp
26th May 2010, 15:21
Have you tried to increase the amount of rows in the loop i.e. something like:



:
:
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 ...
}

calhal
26th May 2010, 15:28
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:

QTableWidget: cannot insert an item that is already owned by another QTableWidget

You should rather do something like this:


ui->tabla->setColumnCount(5);
QTableWidgetItem *item;
while ( query4.next() ) {
for (int i = 0; i < 5; ++i) {
item = new QTableWidgetItem();
item->setText(query4.value(i).toString());
ui->tabla->setItem(a,i,item);
}
++a;
}

mmm286
27th May 2010, 08:31
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:


ui->tabla->setColumnCount(5);
QTableWidgetItem *item;
while ( query4.next() ) {
for (int i = 0; i < 5; ++i) {
item = new QTableWidgetItem();
item->setText(query4.value(i).toString());
ui->tabla->setItem(a,i,item);
}
++a;
}


Many thanks! It works!