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. HOw to use QApplication on a separate thread other than the main thread?
Qt 6.11 is out! See what's new in the release blog

HOw to use QApplication on a separate thread other than the main thread?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 6.9k 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.
  • V Offline
    V Offline
    vimarshinikr
    wrote on last edited by
    #1

    Hi all,
    I have a main thread and a processing thread. I want to create an instance of QApplication on the processing thread and not on the main thread. How to do this?
    Secondly,
    If i create widgets on the processing thread would they run as expected?

    Mainly i want to eliminate the use of QApplication in the main thread. Please suggest me on this.

    thanks in advance!

    1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      A lot of Qt classes (all classes derived from QObject, for example) depend on QCoreApplication istance being present. This is why Q*Application is usually the first line in main routine of Qt applications. You will most probably be unable to create a QThread before the QApplication, so I don't think your scenario is possible (maybe it will work if you use STD threads or Boost threads instead).

      The GUI itself should work OK in another thread, though. It is important that all widgets are instantiated in the same thread, but it should not matter whether that thread is the main one.

      (Z(:^

      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by
        #3

        welcome to forum.

        You can create the QApplication insider worker thread and start creating UI. I have enclosed sample for this. But I don't suggest this.

        How about moving heavy computation itself to worker thread ? This refactoring of this code makes good sense.

        1. I feel you should stick standard coding practice of Qt. This kind of code may create kind of confusion for others as code readability.

        2. Since it is cross platform app, some platforms may things differently with threads. We need to worry about this as well.

        Just as Example

        My main(..)

        @int main(int argc, char *argv[])
        {
        WorkerThread thread;
        thread.run();
        QEventLoop a;
        return a.exec();
        }

        void WorkerThread::run(){
        qDebug() << "I am here"<< endl;
        char* argVV= "Thinks";
        int argc = 0;
        QApplication *app = new QApplication(argc,&argVV);
        QPushButton *button = new QPushButton("Hello");
        button->show();
        exec();
        }@

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        DaazD 1 Reply Last reply
        2
        • dheerendraD dheerendra

          welcome to forum.

          You can create the QApplication insider worker thread and start creating UI. I have enclosed sample for this. But I don't suggest this.

          How about moving heavy computation itself to worker thread ? This refactoring of this code makes good sense.

          1. I feel you should stick standard coding practice of Qt. This kind of code may create kind of confusion for others as code readability.

          2. Since it is cross platform app, some platforms may things differently with threads. We need to worry about this as well.

          Just as Example

          My main(..)

          @int main(int argc, char *argv[])
          {
          WorkerThread thread;
          thread.run();
          QEventLoop a;
          return a.exec();
          }

          void WorkerThread::run(){
          qDebug() << "I am here"<< endl;
          char* argVV= "Thinks";
          int argc = 0;
          QApplication *app = new QApplication(argc,&argVV);
          QPushButton *button = new QPushButton("Hello");
          button->show();
          exec();
          }@

          DaazD Offline
          DaazD Offline
          Daaz
          wrote on last edited by
          #4

          @dheerendra god bless you my friend you just saved our school projet.

          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