使用while循环时不出现窗口

编程入门 行业动态 更新时间:2024-10-25 22:35:56
本文介绍了使用while循环时不出现窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-26 03:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!