禁止警告“未在main()线程中创建QApplication". [英] Suppress warning "QApplication was not created in main() thread"

查看:257
本文介绍了禁止警告“未在main()线程中创建QApplication".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个基于Qt的网络库,以供未运行Qt事件循环的应用程序使用,而这些应用程序不一定是Qt应用程序.通过根据

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()?

这可以很好地工作,但是会使Qt感到不安(我担心我会尝试在主线程之外操作GUI,但这是行不通的,但我没有),因此它会输出警告:警告:QApplication不是在main()线程中创建的.

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.

我想抑制该警告,否则该警告将被打印到X11控制台,并且很可能导致我的用户输入许多不必要的缺陷.但是,我只想抑制 THIS 错误,因为出于某些合法目的使用 qDebug 并希望看到将来的警告.有没有办法像某种Qt #pragma 一样?

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?

在此之前,有人问过类似的问题:,但答案基本上只是代码审查,没有任何有意义的想法来抑制警告.

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.

推荐答案

之所以出现此问题,是因为您在创建QApplication之前接触了Qt API(在主线程中,或者只是在 some 线程中).你不能那样做.特别是,您正在创建某种QObject ,该对象在Qt中设置了Qt本身应该作为 main 线程的地方.

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.

在创建QApplication之前,唯一允许使用的Qt API是明确记录在那种情况下安全的Qt API.

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.

所以:不要那样做.首先构建一个QCoreApplication ,然后您就可以自由了.

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

这篇关于禁止警告“未在main()线程中创建QApplication".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆