(更新)QT QML 5.6 -什么原因导致这个警告“QApplication不是在main()线程中创建的”?

时间:2021-12-04 21:01:04

[UPDATE] OK, I am updating my previous question. At first I thought the warning pops up when I remove widgets from the .pro file - which would have been peculiar behavior. After digging down, I ended up with a completely empty application and the problem still persists. My application looks like this:

[更新]好,我正在更新我之前的问题。起初,我认为当我从.pro文件中删除小部件时,警告就会出现——这将是一种奇怪的行为。在向下挖掘之后,我得到了一个完全空的应用程序,这个问题仍然存在。我的应用程序是这样的:

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    return app.exec();
}    

Based on other posts with the similar kind of problem, I learnt that QApplicationneeds to be the first thing to be initialized. In this case there is NOTHING else in the application. How is this warning still popping up?

基于其他类似问题的帖子,我了解到QApplicationneeds是要初始化的第一个东西。在本例中,应用程序中没有其他内容。这个警告是怎么冒出来的?

W/ (16992): (null):0 ((null)): WARNING: QApplication was not created in the main() thread.

W/ (16992): (null):0 ((null)):警告:QApplication不是在main()线程中创建的。

I am compiling the application directly on my Android device using the Android for x86 (GCC 4.9, Qt 5.6.0) kit.

我正在使用Android的x86 (GCC 4.9, Qt 5.6.0)工具包直接在我的Android设备上编译应用程序。

---- OLD QUESTION\Start ----

- - - - -老问题\——开始

Currently developing an Android app based on Qt 5.6 (C++ and QML). As the UI is based on QtQuick, I removed 'widgets' from the pro.file.

目前正在开发基于Qt 5.6 (c++和QML)的Android应用程序。由于UI是基于QtQuick的,所以我从pro.file中删除了“widgets”。

QT += core qml quick widgets network svg xml gui    

this lead to the warning:

这导致了警告:

WARNING: QApplication was not created in the main() thread.    

and also... as soon as i instantiate QQmlEngine in main() (of course after creating QApplication) this warning is also shown:

和也……一旦我在main()中实例化了QQmlEngine(当然在创建QApplication之后),这个警告也会显示:

 QObject: Cannot create children for a parent that is in a different thread.
(Parent is QQmlDebuggerServiceFactory(0x65fffcd0), parent's thread is QThread(0x5d449f10), current thread is QThread(0x65183000)    

Apparently, the application starts in another thread? and main() in another? as soon as I put 'widgets' in the .pro file, both errors did not show up anymore. I dont really get the correlation between the two things. P.S. not really relevant at this stage of the program but i am also not creating any new threads in my application. This is how my main() looks like:

显然,应用程序从另一个线程开始?和主要()在另一个吗?当我在.pro文件中放入“小部件”时,两个错误都没有出现。我不知道这两者之间的关系。P.S.在这个程序的这个阶段并不是很重要,但是我也没有在我的应用程序中创建任何新的线程。这就是我的main()的样子:

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);

   qmlRegisterUncreatableType<MainFrame>("PSGApp", 1, 0, "MainFrame", "");

   MainFrame m_MainFrame;
   QQmlEngine engine;

   engine.rootContext()->setContextProperty("q_MainFrame",             &m_MainFrame);
   engine.rootContext()->setContextProperty("Ctr",                     m_MainFrame.c());
   engine.rootContext()->setContextProperty("Dev",                     m_MainFrame.c()->dev());
   engine.rootContext()->setContextProperty("Def",                     m_MainFrame.c()->dev()->_def());
   engine.rootContext()->setContextProperty("ModelUdpDevices",         m_MainFrame.UdpDevices());
   engine.rootContext()->setContextProperty("ModelDashboardDevices",   m_MainFrame.DashboardDevices());
   engine.rootContext()->setContextProperty("ModelZones",              m_MainFrame.c()->dev()->_DevZones());
   engine.rootContext()->setContextProperty("ModelRGParameter",        m_MainFrame.c()->dev()->RegelParameter());
   engine.rootContext()->setContextProperty("ModelSYSParameter",       m_MainFrame.c()->dev()->SysParameter());
   engine.rootContext()->setContextProperty("ModelKOMMParameter",      m_MainFrame.c()->dev()->KommParameter());

   QObject::connect(&app, SIGNAL(applicationStateChanged(Qt::ApplicationState)), &m_MainFrame, SLOT(applicationStateChanged(Qt::ApplicationState)));
   QObject::connect(&engine, SIGNAL(quit()), &app, SLOT(quit()));

   QQmlComponent component(&engine,QUrl(QStringLiteral("qrc:/qml/main.qml")));
   component.create();

   return app.exec();
}    

---- OLD QUESTION\End ----

- - - - -老问题\——结束

2 个解决方案

#1


0  

QApplication depends on the widgets module. Use QGuiApplication instead.

QApplication取决于小部件模块。使用QGuiApplication代替。

#2


0  

found the bug. An unused file was still included in the project (even though not #includeed in the code) and it had a global instance of QTranslator. As it is clear from various other (similar) threads, QApplication should be the first QObject to be initialized in main(). Thats why main() was not in the parent thread because QTranslator was initialized before the execution of main().

发现错误。一个未使用的文件仍然包含在项目中(尽管代码中没有包含#include),并且它有一个QTranslator的全局实例。从其他(类似的)线程中可以看出,QApplication应该是在main()中初始化的第一个QObject。这就是为什么main()不在父线程中,因为QTranslator在main()执行之前被初始化。

Such a silly mistake took up an entire day. Peace!

这样一个愚蠢的错误占用了整整一天的时间。和平!

#1


0  

QApplication depends on the widgets module. Use QGuiApplication instead.

QApplication取决于小部件模块。使用QGuiApplication代替。

#2


0  

found the bug. An unused file was still included in the project (even though not #includeed in the code) and it had a global instance of QTranslator. As it is clear from various other (similar) threads, QApplication should be the first QObject to be initialized in main(). Thats why main() was not in the parent thread because QTranslator was initialized before the execution of main().

发现错误。一个未使用的文件仍然包含在项目中(尽管代码中没有包含#include),并且它有一个QTranslator的全局实例。从其他(类似的)线程中可以看出,QApplication应该是在main()中初始化的第一个QObject。这就是为什么main()不在父线程中,因为QTranslator在main()执行之前被初始化。

Such a silly mistake took up an entire day. Peace!

这样一个愚蠢的错误占用了整整一天的时间。和平!