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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With