Suppress warning "QApplication was not created in main () thread"

I have created a Qt-based network library for use with applications that do not work with the Qt event loop and which are not necessarily Qt applications other than Qt. This was made possible by creating an instance of QCoreApplication in the response thread from Is it possible to create local event loops without calling QApplication :: exec ()?

This works fine, but it upsets Qt (I suppose it is worried that I will try to manipulate the GUI outside the main thread, which will not work, but I'm not sure), and therefore it prints a warning: WARNING: QApplication was not created in main() thread .

I would like to suppress this warning, which would otherwise be printed on the X11 console and most likely will force my users to introduce a bunch of unnecessary flaws. However, I would just like to suppress this error as I use qDebug for some legitimate purposes and want to see future warnings. Is there any way to do this like some kind of Qt #pragma ?

EDIT:

A similar question was asked before: Qt console application "WARNING: QApplication was not created in the main () thread , but the answer was basically just a code review without any meaningful ideas to suppress the warning.

+6
source share
1 answer

The problem arises because before creating a QApplication, you touch the Qt API (in the main thread or only in some thread). You cannot do this. In particular, you create some kind of QObject that defines somwhere in Qt, which Qt itself should consider as the main thread.

The only Qt APIs you allowed to use before creating QApplication are those that are explicitly documented for security in this scenario.

So: do not do this. Create a QCoreApplication as the first thing , then you can freely.

+8
source

Source: https://habr.com/ru/post/981049/


All Articles