Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget::repaint: Recursive repaint detected when updating progress bar

My Qt application has multiple threads. One of which calls ui->SyncUI(), where ui is an object of class Interface : public QMainWindow and

void Interface::SyncUI() {
QWidget* bar_widget = ui.tableWidget->cellWidget(0,4);
QProgressBar* bar_widget2 = dynamic_cast <QProgressBar*> (bar_widget);
bar_widget2->setValue( (int)percentage );
}

This causes a runtime error :

QWidget::repaint: Recursive repaint detected

I found this https://qt-project.org/forums/viewthread/24921 but I don't quite understand why setting the bar widget value from anther thread is illegal.

Thanks!

like image 508
user2926577 Avatar asked Sep 08 '14 21:09

user2926577


1 Answers

You should never access widgets and GUI related things directly from a thread other than the main thread. Also calling functions from an object in an other thread directly is illegal and leads to crashes and undefined behavior.

The correct way to update the progress bar is using signal-slot mechanism. Just connect a signal from the thread to a slot of your widget which updates the progress bar. Every time you want to set a new value, just emit the signal. The signal could also have an argument containing the progress percentage.

like image 189
Nejat Avatar answered Nov 01 '22 21:11

Nejat