QWidget :: repaint: recursive redraw detected when updating the progress bar

There are several threads in my Qt application. One of them calls ui->SyncUI() , where ui is a class Interface : public QMainWindow object 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 results in a runtime error:

QWidget :: repaint: recursive review found

I found this https://qt-project.org/forums/viewthread/24921 but I do not quite understand why setting the value of the bar widget from another thread is illegal.

Thanks!

+6
source share
1 answer

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 another thread directly is illegal and leads to crashes and undefined.

The correct way to update the progress bar is to use the signal slot mechanism. Just connect the signal from the stream to the slot of your widget, which updates the progress bar. Each time you want to set a new value, simply issue a signal. The signal may also contain an argument containing the percentage of completion.

+9
source

Source: https://habr.com/ru/post/974999/


All Articles