scdmb
scdmb

Reputation: 15621

Is it legal to create QCoreApplication object not in main()'s thread?

Following this code:

#include <QDebug>
#include <QCoreApplication>
#include <QThread>
#include <thread>

int main(int argc, char *argv[])
{
  // qDebug() << "Whatever"; <- uncommenting this makes the constructor to print a warning
  std::thread thread([&]()
     {
        QCoreApplication app(argc, argv);
        qDebug() << "main thread is " << app.thread();
     });
  thread.join();
  qDebug() << "current thread is " << QThread::currentThread();

  return 0;
}

It compiles and doesn't crash, no warnings during runtime. QCoreApplication is created not in main()'s thread.

However, if the qDebug() function call is uncommented before creating QCoreApplication object, there is printed a warning:

WARNING: QApplication was not created in the main() thread.

Is it legal to create QCoreApplication not in main()'s thread?

Upvotes: 3

Views: 691

Answers (1)

skypjack
skypjack

Reputation: 50550

As from the documentation, it is legal, but not recommended:

In general, we recommend that you create a QCoreApplication, QGuiApplication or a QApplication object in your main() function as early as possible

Upvotes: 2

Related Questions