Qt: Is there notification when event loop starts?

前端 未结 3 2009
一向
一向 2021-02-19 00:19

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

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

    ... A sep         


        
相关标签:
3条回答
  • 2021-02-19 00:32

    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.

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

    0 讨论(0)
  • 2021-02-19 00:34

    Since emitted signals don't get lost when the event loop is not yet running, your thread may not necessarily need to know when your window is ready.
    Your thread could start sending signals to the window right away but it will only receive signals from the window when the event loop is running.

    0 讨论(0)
  • 2021-02-19 00:37

    You can do it in the following order:

    QApplication app(argc, argv);
    Mainwinwdow mainWin;
    QThread yourThread;
    
    //connect the signals from the thread to the mainWin here
    
    mainWin.Init();
    mainWin.show();
    
    yourThread.start();
    
    return app.exec();
    
    0 讨论(0)
提交回复
热议问题