[英](UPDATE) QT QML 5.6 - What causes this warning “QApplication was not created in the main() thread”?
[更新]好的,我正在更新上一個問題。 起初,我以為從.pro文件中刪除widgets
時會彈出警告-這本來是奇怪的行為。 深入研究后,我得到了一個完全空的應用程序,問題仍然存在。 我的應用程序如下所示:
#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.
而且...一旦我在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)
顯然,該應用程序在另一個線程中啟動? 和main()在另一個? 一旦我將.widgets放入.pro文件中,兩個錯誤均不再顯示。 我真的沒有得到兩件事之間的關聯。 PS在程序的此階段並不真正相關,但是我也沒有在應用程序中創建任何新線程。 這是我的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();
}
----舊問題\\結束----
QApplication
取決於widgets
模塊。 請改用QGuiApplication
。
發現了錯誤。 一個未使用的文件仍包含在項目中(即使代碼中未包含#include
),並且它具有QTranslator
的全局實例。 從其他各種(類似)線程可以清楚地看出, QApplication
應該是要在main()
初始化的第一個QObject
。 這就是為什么main()
不在父線程中的原因,因為QTranslator
在執行main()
之前已初始化。
這樣一個愚蠢的錯誤占據了整整一天。 和平!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.