简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM