Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Run QApplition in thread!
Qt 6.11 is out! See what's new in the release blog

Run QApplition in thread!

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved