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. create new QApplication in different thread
Qt 6.11 is out! See what's new in the release blog

create new QApplication in different thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 203 Views 2 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.
  • C Offline
    C Offline
    csab6597
    wrote last edited by csab6597
    #1

    running this very simple code

    int main(int argc, char** argv) {
        qDebug() << "create app 1" << QThread::currentThreadId();
        QApplication* a = new QApplication(argc, argv);
        qDebug() << "delete app 1";
        delete a;
    
        qDebug() << "create app 2" << QThread::currentThreadId();
        QApplication* b = new QApplication(argc, argv);
        qDebug() << "delete app 2";
        delete b;
    
        auto func = [&] {
            qDebug() << "create app 3" << QThread::currentThreadId();
            QApplication* c = new QApplication(argc, argv);
            qDebug() << "delete app 2";
            delete c;
        };
        std::thread t(func);
        t.join();
    
        qDebug() << "done";
        return 0;
    }
    

    I get the output

    create app 1 0x420c
    delete app 1
    create app 2 0x420c
    delete app 2
    create app 3 0x75fc
    WARNING: QApplication was not created in the main() thread.
    Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)
    

    Now I do not understand:
    I read there can be only one instance of QApplication - Ok I take care of that
    I read a QApplication can be created on a different thread, which then becomes sort of the application thread
    Then why is this example not possible?

    Qt 6.11.1, Windows 11

    Ronel_qtmasterR JKSHJ 2 Replies Last reply
    1
    • C csab6597

      running this very simple code

      int main(int argc, char** argv) {
          qDebug() << "create app 1" << QThread::currentThreadId();
          QApplication* a = new QApplication(argc, argv);
          qDebug() << "delete app 1";
          delete a;
      
          qDebug() << "create app 2" << QThread::currentThreadId();
          QApplication* b = new QApplication(argc, argv);
          qDebug() << "delete app 2";
          delete b;
      
          auto func = [&] {
              qDebug() << "create app 3" << QThread::currentThreadId();
              QApplication* c = new QApplication(argc, argv);
              qDebug() << "delete app 2";
              delete c;
          };
          std::thread t(func);
          t.join();
      
          qDebug() << "done";
          return 0;
      }
      

      I get the output

      create app 1 0x420c
      delete app 1
      create app 2 0x420c
      delete app 2
      create app 3 0x75fc
      WARNING: QApplication was not created in the main() thread.
      Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)
      

      Now I do not understand:
      I read there can be only one instance of QApplication - Ok I take care of that
      I read a QApplication can be created on a different thread, which then becomes sort of the application thread
      Then why is this example not possible?

      Qt 6.11.1, Windows 11

      Ronel_qtmasterR Offline
      Ronel_qtmasterR Offline
      Ronel_qtmaster
      wrote last edited by
      #2

      @csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication

      JKSHJ 1 Reply Last reply
      0
      • C csab6597

        running this very simple code

        int main(int argc, char** argv) {
            qDebug() << "create app 1" << QThread::currentThreadId();
            QApplication* a = new QApplication(argc, argv);
            qDebug() << "delete app 1";
            delete a;
        
            qDebug() << "create app 2" << QThread::currentThreadId();
            QApplication* b = new QApplication(argc, argv);
            qDebug() << "delete app 2";
            delete b;
        
            auto func = [&] {
                qDebug() << "create app 3" << QThread::currentThreadId();
                QApplication* c = new QApplication(argc, argv);
                qDebug() << "delete app 2";
                delete c;
            };
            std::thread t(func);
            t.join();
        
            qDebug() << "done";
            return 0;
        }
        

        I get the output

        create app 1 0x420c
        delete app 1
        create app 2 0x420c
        delete app 2
        create app 3 0x75fc
        WARNING: QApplication was not created in the main() thread.
        Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)
        

        Now I do not understand:
        I read there can be only one instance of QApplication - Ok I take care of that
        I read a QApplication can be created on a different thread, which then becomes sort of the application thread
        Then why is this example not possible?

        Qt 6.11.1, Windows 11

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote last edited by
        #3

        @csab6597 said in create new QApplication in different thread:

        I read a QApplication can be created on a different thread, which then becomes sort of the application thread

        Yes, that's right.

        If you don't create a and b first, then your c will not produce any warnings and your func thread will be considered the "main thread".

        Then why is this example not possible?

        This is a current limitation in Qt. QGuiApplication creates some internal QObjects that never get destroyed (here is one example: https://qt-project.atlassian.net/browse/QTBUG-141714), so it is currently not possible to re-create QGuiApplication/QApplication in a different thread from your first instance.

        In contrast, your example should work fine if you use QCoreApplication.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4
        • Ronel_qtmasterR Ronel_qtmaster

          @csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote last edited by
          #4

          @Ronel_qtmaster said in create new QApplication in different thread:

          @csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication

          There are perfectly valid use-cases to destroy a QApplication and recreate it in another thread. For example, when Qt is used to implement a plugin for a non-Qt app.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3
          • TimTom420T Offline
            TimTom420T Offline
            TimTom420
            wrote last edited by
            #5

            Thanks for the clarification. That explains the behavior.

            The limitation makes sense for my use case (Qt used as a plugin inside a non-Qt application). I had interpreted the documentation to mean that, as long as only one QApplication instance exists at a time, recreating it in a different thread would be supported.

            Knowing that QGuiApplication leaves behind internal objects tied to the first thread, making cross-thread recreation currently impossible, answers my question. I'll adjust the design accordingly.

            Thanks for the detailed explanation and the Qt bug reference.

            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