Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

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?

like image 601
scdmb Avatar asked Dec 05 '25 18:12

scdmb


1 Answers

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

like image 102
skypjack Avatar answered Dec 08 '25 12:12

skypjack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!