[SOLVED] live update to qtablewidget not working
-
hi, i'm trying to do live update of data to table for every 2 second delay. here is my code
@
QTableWidget *table=new QTableWidget(this);
table->setGeometry(20,20,200,200);
table->setRowCount(5);
table->setColumnCount(1);
int m=0,n=0;
for(int i=0;i<5;i++)
{
table->setItem(m,n,new QTableWidgetItem("hello"));
qDebug()<<" sleep for 2s ";
sleep(2);
m++;
}
@
qdebug prints data for every 2 second delay but in qtablewidget ,data is not getting updated for every 2 second. table gets filled at a time after "for " loop ends.
can anyone please tel me how to achieve this.thanks.
-
Hi,
Any loop or blocking call (like sleep) in the main thread will prevent the GUI from updating itself.
You could use a QTimer and a slot instead of a loop.
-
alexisdm:: i tried using qtimer in slot function i need to update the qtablewidget continously with some delay but by using qtimer i can not see the table getting update.
-
Without seeing the new code, I can't know what you might have done wrong.
But basically, you should have something like:
@table = new QTableWidget(this);
table->setColumnCount(1);
count = 5;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(updateSlot()));
timer->start(2000);
...void YourClass::updateSlot()
{
Q_ASSERT(count>=0);
--count;
if(!count)
timer->stop();
int row = table->rowCount();
table->insertRow(row);
table->setItem(row, 0, new QTableWidgetItem("hello"));
}
@
table, count and timer should all be members of the class so that they can be accessed in the slot. -
thanks alexisdm its working.