Using custom objects in widget-based QML
-
I have a Widget based application and one part is showing a QML file.
It works fine with built in types.Want to load an external QML file (e.g. myScreen.qml) with custom QML objects.
Let's says there are two custom objects:- BoxInResources (it shall be contained in the qml.qrc)
- BoxInFile (it shall be contained in a file named BoxInFile.qml)
The myScreen.qml shall look something like:
import QtQuick 2.7 import QtQuick.Controls 2.0 Rectangle { color: "green" Text { text: "TEST" } BoxInResources {} BoxInFile{} }
First, I've tried with the BoxInResources. Added it in source files, qml.qrc, as QML. But nothing did the trick.
Each time at load it replies: BoxInResource is not a typeI also get the error if the file is in the working path.
How can I link it to the executable so it will be available as a type?This is a rephrased question of https://forum.qt.io/topic/86789/using-a-qml-file-at-runtime
-
I have a Widget based application and one part is showing a QML file.
It works fine with built in types.Want to load an external QML file (e.g. myScreen.qml) with custom QML objects.
Let's says there are two custom objects:- BoxInResources (it shall be contained in the qml.qrc)
- BoxInFile (it shall be contained in a file named BoxInFile.qml)
The myScreen.qml shall look something like:
import QtQuick 2.7 import QtQuick.Controls 2.0 Rectangle { color: "green" Text { text: "TEST" } BoxInResources {} BoxInFile{} }
First, I've tried with the BoxInResources. Added it in source files, qml.qrc, as QML. But nothing did the trick.
Each time at load it replies: BoxInResource is not a typeI also get the error if the file is in the working path.
How can I link it to the executable so it will be available as a type?This is a rephrased question of https://forum.qt.io/topic/86789/using-a-qml-file-at-runtime
@gkavrecic
this is pretty well documented.
Please read this. -
Thanks for that, but the link describes integrating C++ objects into QML.
This is the next step.I would like to use QML objects. (hence the BoxInFile part)
I managed to add BoxInResource in a pure QML application. -
@gkavrecic
this is pretty well documented.
Please read this. -
Thanks for that, but the link describes integrating C++ objects into QML.
This is the next step.I would like to use QML objects. (hence the BoxInFile part)
I managed to add BoxInResource in a pure QML application.@gkavrecic
So you can either import the folder containing your type/component directly from qrc within QML:import "qrc:/path/containing/components"
If it's not working try:
import ":/path/containing/components"
or alternatively:
// C++ qmlRegisterType(QUrl("qrc:///path/to/MyComponent.qml")), "MyTypes", 1, 0, "MyComponent"); ... // QML import MyTypes 1.0