Solved Stop QCoreApplication running in C++11 thread
-
Hi,
I've got QCoreApplication running in C++11 thread. It created there and it call exec() to stay in event loop. This thread is created to process dbus communicaiton. Rest of application is unaware of Qt.
Now I would like to kill this thread safely when whole application is closed.
How to do it? Any useful techniques? Callback? -
-
Yes, I know.
Below works as class Communicate exist as long as main class. I know it not nice - calling from one thread to other.class Communicate { .... void run(); void * qt_app_ptr; } void run() { thread_holder = std::thread([this, qt_app_ptr]{ QCoreApplication app(argc, nullptr); qt_app_ptr = &app; (.....) app.exec() }); } Communicate::~Communicate() { static_cast<QCoreApplication*>(qt_app_ptr)->quit(); thread_holder.join(); }
-
No need to store a pointer to the app. Either use the static function QCoreApplication::exit(0) or qApp->quit().
-
in generic sense: send signal to thread, trap signal, execute QApplication::quit