2016-09-14 31 views
0

[更新]好的,我正在更新我以前的问题。起初,我认为当我从.pro文件中删除widgets时会弹出警告 - 这将是一种奇怪的行为。深入挖掘后,我最终得到了一个完全空白的应用程序,问题仍然存在。我的应用程序是这样的:(更新)QT QML 5.6 - 导致此警告的原因“QApplication未在main()线程中创建”?

#include <QApplication> 

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

    return app.exec(); 
}  

基于采用类似的问题其他职位,我得知QApplication必须进行初始化的第一件事。在这种情况下,应用程序中没有别的。这个警告是如何弹出的?

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

我使用Android for x86 (GCC 4.9, Qt 5.6.0)套件直接编译我的Android设备上的应用。

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

目前正在开发基于Qt 5.6(C++和QML)Android应用程序。由于UI基于QtQuick,我从pro.file中删除了“小部件”。

QT += core qml quick widgets network svg xml gui  

这导致了警告:

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

也......只要我实例QQmlEngine在main()这个警告也显示(当然创造的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)  

显然,应用程序在另一个线程中启动?和main()在另一个?只要我把'小部件'放在.pro文件中,这两个错误都不会再显示出来。我真的不知道这两件事之间的关系。 P.S.在程序的这个阶段没有真正的相关性,但我也没有在我的应用程序中创建任何新线程。 这就是我的主要()看起来像:

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(); 
}  

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

回答

0

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

这样一个愚蠢的错误花了整整一天。和平!

0

QApplication取决于widgets模块。改为使用QGuiApplication

+0

我已经更新了这个问题。问题不是.pro文件中的“widgets”模块。即使应用程序完全空白,问题仍然存在。检查编辑的问题。为什么不在main()线程中创建QApplication,即使没有别的东西? – spikeyeddy

相关问题