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. QApplication was not created in the main() thread
Qt 6.11 is out! See what's new in the release blog

QApplication was not created in the main() thread

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 10.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.
  • T Offline
    T Offline
    terenty
    wrote on last edited by
    #1

    Hello everyone,
    Could someone explain why in the following scenario some buggy behavior is present(QTextEdit window is not automatycally updated):

    @
    #include <qapplication.h>
    #include <qtextedit.h>
    #include <qrunnable.h>
    #include <qthread.h>
    #include <qthreadpool.h>

    int main(int argc, char *argv[])
    {
    class GUIinThread : public QRunnable
    {
    void run()
    {
    int argc = 1;

    QByteArray argvStr = QString("").toLatin1();
    char* argv = argvStr.data();

    QApplication a(argc, &argv);

    QTextEdit textEdit;
    textEdit.show();

    a.exec();
    }
    };

    GUIinThread *createGUI = new GUIinThread();
    QThreadPool::globalInstance()->setExpiryTimeout(-1);
    QThreadPool::globalInstance()->start(createGUI);

    while(true); // prevent main() from return
    return 0;-
    }@

    And why is this so important to create QApplication in the main thread?
    I'm using Qt5.1.1 under WIn7
    Thnaks in advance!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, QApplication and all other QWidget can only be used in main thread.

      http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        terenty
        wrote on last edited by
        #3

        Hi, 1+1=2,
        Thanks for the reply, but I still don't get whats so special about main thread and why we can't have another thread devoted to GUI other than main as long as we don't make any cross-thread gui interactions?

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Some platforms restrict GUI interactions to the first thread spawned by a process: http://www.dribin.org/dave/blog/archives/2009/02/01/main_thread_apis/

          Qt is a cross-platform library, and thus it needs to follow the platforms' rules.

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            terenty
            wrote on last edited by
            #5

            JKSH,
            Thanks, it's finally starting to make sense. That OS X must be a really "fun" thing to deal with... Too bad it has such a frustrating influence on Qt...

            I have a console app that makes a lot of computation in it's main() and log iteration data to a file, i wanted to log into quickly made fully responsible gui running in parallel to main() so it would be a little change to main() but faced this limitation :(

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              You're welcome. Yea, creating a cross-platform library is hard work. But I think the benefits outweigh the downsides :)

              Hmm... could you rename your current main() into something else, and just call it from another thread? You can create a new main(), create your GUI, and start a new thread to run the old main().

              P.S. See http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html for a comparison of different ways to use threads in Qt. QRunnable is not suitable for permanent threads.

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

              1 Reply Last reply
              0
              • T Offline
                T Offline
                terenty
                wrote on last edited by
                #7

                Thats a bunch of usfefull links you've got :) (not really too easy to find them on your own)
                Ive been stepping into Qt source all morning but was unable to find the difference to QEventDispatcherWin32 if created not on the main thread...
                So I guess Im gonna have to go with your suggestion of renaming my main() etc...
                Thanks again :)

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  You're welcome :) All the best!

                  (I actually found the first link after reading your post, by searching "GUI main thread" in Google)

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

                  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