Singleton QML failed to load as module
-
Hi,
I am using Qt 5.15.0 and created a singleton QML file and tried to load it as a module using qmldir.
But unfortunately, I am receiving an error when QQmlApplicationEngine is trying to load the component.
What I am doing wrong and getting this frustrating output? Any ideas what's happening?
Below you can find all the information needed to help me out.
Thanks!
Application output
QQmlApplicationEngine failed to load component <Unknown File>:12:5: Composite Singleton Type TestMe is not creatable.
TestMe.qml
pragma Singleton import QtQuick 2.0 Item { readonly property string value: "value" }
main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") TestMe { value: "new value" } }
qmldir
module base singleton TestMe 1.0 TestMe.qml
qmake
QML_IMPORT_PATH = $$PWD/imports
main.cpp
QQmlApplicationEngine engine; engine.addImportPath("qrc:/imports"); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url);
Project structure screenshot
-
AFAIK only
QtObject
modules can be singletons notItem
.
Change TestMe.qml fromItem
toQtObject
and it should works as expected.
cf . https://wiki.qt.io/Qml_StylingThank you for your prompt reply. In the beginning, I had it with
QtObject
and changed it back now.But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
@nsourl said in Singleton QML failed to load as module:
But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
Sorry, I read too quickly your first post.
As you have definedTestMe
as singleton, you can not create any instance of it, because its already exists!Change your main.qml as follow, it should than work:
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") + TestMe.value Component.onCompleted: TestMe.value ="new value" }
-
Hi,
I am using Qt 5.15.0 and created a singleton QML file and tried to load it as a module using qmldir.
But unfortunately, I am receiving an error when QQmlApplicationEngine is trying to load the component.
What I am doing wrong and getting this frustrating output? Any ideas what's happening?
Below you can find all the information needed to help me out.
Thanks!
Application output
QQmlApplicationEngine failed to load component <Unknown File>:12:5: Composite Singleton Type TestMe is not creatable.
TestMe.qml
pragma Singleton import QtQuick 2.0 Item { readonly property string value: "value" }
main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") TestMe { value: "new value" } }
qmldir
module base singleton TestMe 1.0 TestMe.qml
qmake
QML_IMPORT_PATH = $$PWD/imports
main.cpp
QQmlApplicationEngine engine; engine.addImportPath("qrc:/imports"); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url);
Project structure screenshot
@nsourl said in Singleton QML failed to load as module:
What I am doing wrong and getting this frustrating output? Any ideas what's happening?
AFAIK only
QtObject
modules can be singletons notItem
.
Change TestMe.qml fromItem
toQtObject
and it should works as expected.
cf . https://wiki.qt.io/Qml_Styling -
@nsourl said in Singleton QML failed to load as module:
What I am doing wrong and getting this frustrating output? Any ideas what's happening?
AFAIK only
QtObject
modules can be singletons notItem
.
Change TestMe.qml fromItem
toQtObject
and it should works as expected.
cf . https://wiki.qt.io/Qml_StylingAFAIK only
QtObject
modules can be singletons notItem
.
Change TestMe.qml fromItem
toQtObject
and it should works as expected.
cf . https://wiki.qt.io/Qml_StylingThank you for your prompt reply. In the beginning, I had it with
QtObject
and changed it back now.But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
-
AFAIK only
QtObject
modules can be singletons notItem
.
Change TestMe.qml fromItem
toQtObject
and it should works as expected.
cf . https://wiki.qt.io/Qml_StylingThank you for your prompt reply. In the beginning, I had it with
QtObject
and changed it back now.But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
@nsourl said in Singleton QML failed to load as module:
But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
Sorry, I read too quickly your first post.
As you have definedTestMe
as singleton, you can not create any instance of it, because its already exists!Change your main.qml as follow, it should than work:
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") + TestMe.value Component.onCompleted: TestMe.value ="new value" }
-
@nsourl said in Singleton QML failed to load as module:
But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.
Sorry, I read too quickly your first post.
As you have definedTestMe
as singleton, you can not create any instance of it, because its already exists!Change your main.qml as follow, it should than work:
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") + TestMe.value Component.onCompleted: TestMe.value ="new value" }
Sorry, I read your too quickly your first post.
As you have definedTestMe
as singleton, you can not create any instance of it, because its already exists!Change your main.qml as follow, it should than work:
import QtQuick 2.15 import QtQuick.Window 2.15 import base 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") + TestMe.value Component.onCompleted: TestMe.value ="new value" }
Oh yeah you are absolutely right, since it's a singleton the instance already exists!
Of course now it works as expected! :)
Thank you for your help!