Run QApplition in thread!



  • Hello,
    The problem is: I have two projects write by Qt,one is GUI and the other is a console. Now I want to merge these 2 projects,so I write two interfaces like "int gui_start(int argc, char** argv); int console_start();" to start GUI and Console;
    Here is the code:
    @
    std::thread g_Thread = NULL;
    static int work_thread(int argc, char
    * argv)
    {
    QApplication app(argc, argv);
    MainWindow *mainWindow= new MainWindow ;
    mainWindow->show();

    app.exec();
    return 0;
    

    }

    static int gui_start(int argc, char**argv)
    {
    if(!g_Thread) {
    g_Thread = new std::thread(work_thread, argc, argv);
    }
    return 0;
    }
    static int console_start()
    {
    // same as gui_start;
    }

    int main(int argc, char**argv)
    {
    gui_start(argc, argv);
    console_start();
    getchar();
    return 0;
    }
    @

    When I running the program,I got "WARNING: QApplication was not created in the main() thread." the gui show normally,but the console thread not work.
    Here is the debug infomations:
    @
    QObject::startTimer: QTimer can only be used with threads started with QThread
    QObject::startTimer: QTimer can only be used with threads started with QThread
    QObject::startTimer: QTimer can only be used with threads started with QThread
    @

    1.Does QApplication must run in main thread?
    2.Is there a way to use two QApplications in one main(I tried write them in two libraries and export two interfaces it also not work).
    Thank you!



  • QApplication must run in main thread (GUI thread).
    Similar restriction is true for almost all windowing systems including Java swt.
    There is only one QApplication for one process.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.