Qt:事件循环开始时是否有通知? [英] Qt: Is there notification when event loop starts?

查看:58
本文介绍了Qt:事件循环开始时是否有通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这种main()的Qt应用程序...

I have a Qt application with this kind of main()...

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow   mainWin;

    ... A separate, non-GUI thread is launched here

    mainWin.Init();
    mainWin.show();

    app.exec();
}

在mainWin之前创建的另一个线程需要知道何时可以开始与mainWin通信.但是由于mainWin使用Qt信号,插槽,计时器等,因此直到事件循环运行之前(通过exec()),它才真正准备好开始摇摆.

This other thread that is created before the mainWin needs to know when it can start communicating with the mainWin. But since the mainWin uses Qt signals, slots, timers, etc, it's not truly ready to rock until the event loop is running (via exec()).

我的问题是:事件循环开始时是否发出一些信号或事件?

My question is: is there some signal or event that is emitted when the event loop has started?

考虑一下.在mainWin.Init()中,您可以创建类似QTimer的代码,甚至可以调用.start()将其启动.但它实际上不会运行并触发事件,直到调用exec()为止.这就是为什么我需要知道事件循环何时真正开始的原因.

Consider this. In mainWin.Init(), you can create something like a QTimer and even call .start() to kick it off. But it won't actually be run and trigger events until exec() has been called. This is why I need to know when the event loop has truly started.

推荐答案

您可以在 exec()调用之前向窗口发送信号.这将在 app 的信号队列中放置一个条目.当 exec()运行时,信号将被传递,并且您的窗口将知道事件循环正在运行.

You can send a signal to your window before the exec() call. This will place an entry in app's signal queue. When exec() is running, the signal will be delivered and your window will know that the event loop is running.

一种简单的方法是使用连接到窗口类的自定义插槽的 QTimer :: singleShot(0,& mainWin,SLOT(onEventLoopStarted())).

A simple way would be to use QTimer::singleShot(0, &mainWin, SLOT(onEventLoopStarted())); which connects to a custom slot of your window class.

这篇关于Qt:事件循环开始时是否有通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆