Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
6.8.0
-
-
595abf24a (dev), 3c162c592 (6.8), d974feafa (tqtc/lts-6.5)
Description
When you create an object with QQmlComponent::loadFromModule(), it does not work as expected. Here's an example:
// cppitem.hpp #pragma once #include <QtQuick/QQuickItem> class CppItem : public QQuickItem { Q_OBJECT QML_ELEMENT Q_PROPERTY(QString text MEMBER text) private: QString text; auto classBegin() -> void final { qDebug() << "classBegin"; QQuickItem::classBegin(); } auto componentComplete() -> void final { QQuickItem::componentComplete(); qDebug() << "componentComplete" << text; qDebug() << "qmlEngine" << qmlEngine(this); qDebug() << "qmlContext" << qmlContext(this); } };
// main.cpp #include <QtQuick> #include "cppitem.hpp" int main(int argc, char **argv) { QGuiApplication app(argc, argv); QQuickWindow window; QQmlEngine engine; { QQmlComponent component{&engine}; component.setData("import App; CppItem {}", {}); component.createWithInitialProperties(QVariantMap{{"text", "load from data"}}); } { QQmlComponent component{&engine}; component.loadFromModule("App", "CppItem"); component.createWithInitialProperties(QVariantMap{{"text", "load from module"}}); } return 0; }
Here, you create exact same object twice, once with setData(), and once with loadFromModule(). The result should be same but it doesn't.
classBegin componentComplete "load from data" qmlEngine 0xe634cff448 qmlContext 0x25edae2a7e0 classBegin componentComplete "" qmlEngine 0x0 qmlContext 0x0
The result from setData() case is as expected.
On the other hand, the result from loadFromModule() has problems:
1. the default properties ignored.
2. qmlEngine() and qmlContext() returns nullptr