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

1

8

I've created a Qt-based network library for use with applications that are not running a Qt event loop, and which are not necessarily otherwise Qt applications. This was made possible by creating a QCoreApplication instance in a thread per the answer from Is it possible to create local event loops without calling QApplication::exec()?

This works perfectly, but it makes Qt upset (I presume it's worried that I'll try to manipulate a GUI outside of the main thread which wouldn't work, but I'm not), and so it prints a warning: WARNING: QApplication was not created in main() thread.

I'd like to suppress that warning which will otherwise be printed to the X11 console and most likely cause my users to enter a bunch of needless deficiencies. However, I'd like to just supress THIS error, as I use qDebug for some legitimate purposes and want to see future warnings. Is there a way to do this, like some kind of Qt #pragma?

EDIT:

A similar question was asked before here: 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.

Danelaw answered 15/1, 2015 at 12:39 Comment(4)
I think the problem arises because you're touching Qt APIs (in the main thread, or just in some thread) before creating QApplication. You can't do that (modulo stuff that is supposed to be done before QApplication). In particular, you're creating QObjects.Blister
hmm, that creates an interesting chicken-egg problem, because the class I use to create the QCoreApplication thread is itself a QObject because it needs to use signals to communicate with sockets in child threads.Danelaw
Well, just split it? Keep that class with the logic, and create another class that creates QCoreApplication AND an object of your class.Blister
@Blister your two suggestions together did the trick! thanks! If you want to rephrase them as an answer, I'd be happy to accept it.Danelaw
B
11

The problem arises because you're touching Qt APIs (in the main thread, or just in some thread) before creating QApplication. You can't do that. In particular, you're creating a QObject of some kind, which is setting somwhere in Qt what Qt itself should consider as the main thread.

The only Qt APIs you're allowed to use before creating a QApplication are the ones that are explicitely documented to be safe in that scenario.

So: don't do that. Build a QCoreApplication as the first thing, then you're free to go.

Blister answered 15/1, 2015 at 16:57 Comment(3)
Refactoring my code in this form also solved a lot of strange and intermittent issues I had with connections between objects in different threads. I definitely will not ignore this warning again.Danelaw
@Blister is this similar when trying to plot something with matplotlib in python at PyCharm IDE? I started a bounty about it, maybe you can help: #55978877Theodoratheodore
I just realized that just doing from PyQt5 import QtCore, QtWidgets in Python is enough to "touch" the Qt API and thereby making it settle on what's the "main" thread. So I had to move a whole class declaration into another module and then late-load that at runtime, in the code that is run in a side-thread, to make the warning go away.Ingvar

© 2022 - 2024 — McMap. All rights reserved.