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.

Community
  • 1
  • 1
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
  • 1
    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. – peppe Jan 15 '15 at 13:04
  • 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. – Nicolas Holthaus Jan 15 '15 at 13:07
  • 1
    Well, just split it? Keep that class with the logic, and create another class that creates QCoreApplication AND an object of your class. – peppe Jan 15 '15 at 13:08
  • @peppe your two suggestions together did the trick! thanks! If you want to rephrase them as an answer, I'd be happy to accept it. – Nicolas Holthaus Jan 15 '15 at 15:23

1 Answers1

10

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.

peppe
  • 21,934
  • 4
  • 55
  • 70
  • 1
    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. – Nicolas Holthaus Jan 15 '15 at 17:32
  • 1
    @peppe 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: https://stackoverflow.com/questions/55978377/how-to-avoid-pycharm-console-crash-when-plotting-with-matplotlib – Homero Esmeraldo May 22 '19 at 21:28