QWidget :: repaint:更新进度条时检测到递归重画 [英] QWidget::repaint: Recursive repaint detected when updating progress bar
问题描述
我的Qt应用程序具有多个线程.其中一个调用ui->SyncUI()
,其中ui
是class Interface : public QMainWindow
和
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 );
}
这会导致运行时错误:
QWidget :: repaint:检测到递归重绘
QWidget::repaint: Recursive repaint detected
我发现了这个 https://qt-project.org/forums/viewthread/24921 但是我不太明白为什么从花药线程设置bar窗口小部件值是非法的.
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.
谢谢!
推荐答案
永远不要直接从主线程以外的线程访问与小部件和GUI相关的东西.从另一个线程中的对象直接调用函数也是非法的,并导致崩溃和不确定的行为.
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.
这篇关于QWidget :: repaint:更新进度条时检测到递归重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!