问题描述
限时送ChatGPT账号..我正在用 C++ 用 QT 做一些东西.
I am making something in QT in C++.
但是,当我在代码中使用 while(1)
循环时,该窗口永远不会出现.我尝试了很多方法,例如在循环末尾添加 QApplication::processEvents();
,但它不起作用.没有窗户.
However, when I am using a while(1)
loop in the code, the window never appears. I tried many things, such as adding a QApplication::processEvents();
at the end of the loop, but it doesn't work. There is no window.
如何让窗口出现?
示例代码:
MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) {
_ui.setupUi(this);
while(1){
}
}
谢谢
推荐答案
每个小部件构造函数都不应阻塞主消息循环!
Every widget constructor should never block the main message loop!
主消息循环通常如下所示:
The main message loop looks usually like this:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w(nullptr);
w.show();
int r = a.exec();
return r
}
在你的情况下,你的 MainWindow
ctor 永远不会返回,所以 w.show()
永远不会被调用并且 a.exec()
(mainmessgae 循环)永远不会执行.
In your case your MainWindow
ctor never returns, so w.show()
is never called and a.exec()
(main messgae loop) is never executed.
不仅阻塞可能是主窗口构造函数中的一个问题,而且在主消息循环执行之前生成的信号也永远不会被引发.例如,在主窗口 ctor 中建立 TCP/IP 连接将永远不会引发 connected()
信号和关联的插槽.*1
Not only blocking may be a problem in main window ctor, but also signals that are generated before main message loop is executed gets never raised. For an example establishment of an TCP/IP connection within main window ctor will never raise the connected()
signal and associated slots. *1
至少如果主窗口的创建是在主消息循环执行之前,就像在 99% 的情况下一样.
这篇关于使用while循环时不出现窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论