[英]QApplication method setStyleSheet called from other thread causes segmentation fault
[英]Input widget in QApplication in a dedicated thread causes QTimer to be stopped from the wrong thread
由于不同的原因,我必须创建一个不在程序主线程中的QApplication
。 除了清理部分外,这很好用。 一旦main()
完成,我会收到以下通知:
QObject::~QObject: Timers cannot be stopped from another thread
在所有 Qt 小部件和 windows 以及QApplication
都被处理后,这会发生相当长的一段时间。
我将问题缩小到这个最小的例子:
#include <thread>
#include <QApplication>
#include <QLineEdit>
int main(int argc, char * argv[])
{
std::thread t {
[&] {
QApplication app{argc, argv};
QLineEdit w{"Test"};
w.showNormal();
app.exec();
}
};
std::this_thread::sleep_for(std::chrono::seconds{3});
QApplication::instance()->quit();
t.join();
return 0;
}
QSpinBox
而不是QLineEdit
会导致相同的行为。 但是,当我使用QLabel
时,不会发出警告。 所以我怀疑罪魁祸首是负责 cursor 闪烁的计时器,但我在这里可能错了。
退出QApplication
时,有什么方法可以正确停止这个(不可见的)计时器吗?
除了主线程之外,您不能从任何地方访问小部件和其他相关元素。 这在几乎所有 GUI 系统中都很常见(例如 MFC、BGI、...)。 对于 Qt 在文档中有一个提示。 令人惊讶的是,工作线程中甚至不允许使用 QPixmap。
我也很乐意在应用程序的事件循环中手动发布类似 QuitEvent 的内容,而不是直接调用 quit()。
这里是 go:
QMetaObject::invokeMethod(QApplication::instance(), "quit", Qt::QueuedConnection);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.