I use a timer to emit a signal and call a slot function which updates the UI according to new data.
I’ve calculated the time to run that function as being quite short. When the timer is set to 1000ms, the UI responds quite slow.
I tried to move that functionality to a thread but I’m finding it difficult since a lot of the functionality needs to access the UI class’s protected values.
I’m going to try moving the timer to another thread and leave the update functionality in the UI class (main window) but I don’t know if it will help.
Why is the timer causing the UI to be slow and unresponsive? Will a thread be lighter and consume less CPU time? How can I fix this?
initTimer()
{
refreshTimer = new QTimer(this);
connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshDisplay));
refreshTimer->start(1000);
}
refresh slot function called every 1000ms:
void refreshDisplay()
{
ui->tableWidget->setUpdatesEnabled(false);
for(int queue_i = size_1, index = 0; queue_i >= 0; queue_i--, index++)
{
LogInfoItem* logItem = (LogInfoItem*)logDisplayQueue.at(queue_i);
QString BITS_str = bits2Hexs(logItem->BITS);
ui->tableWidget->item(index, 0)->setText(logItem->time);//time
ui->tableWidget->item(index, 1)->setText(logItem->name);//name
ui->tableWidget->item(index, 2)->setText(BITS_str);//BITS
if(queue_i == oldRowItemNo)ui->tableWidget->selectRow(index);
}
ui->tableWidget->setUpdatesEnabled(true);
Q_FOREACH(Page* p, PageInfoList)
{
p->refresh();
}
Q_FOREACH(IconLabel* icl, iconLabelList)
{
icl->refresh();
}
}
What ‘refresh()’ does is just changing icons and texts in the ui according to the data inside. Besides, i’ve made the data static, but still cannot fix it.
(I’ve tested the function, almost no time consuming…)
Thank you, I have used your mehods and finally find the cpu eater, it is setIcon function . I add an if statement before it, and wll not call it if data not changed. Thank you, everyone!