Not able to access members in QML File
-
Not able to set, read using QQmlProperty
error:
QQmlComponent: component is not ready
Property value 0
The program has unexpectedly finished. Please kindly assist.@
-------------qml file------------------------
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")Item { property int someNumber:100 }
}
-------------cpp file------------------------#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QDebug>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QQmlEngine engine; QQmlComponent component(&engine,"qrc:///main.qml"); QObject *object=component.create(); qDebug()<<"Property value"<<QQmlProperty::read(object,"someNumber").toInt(); QQmlProperty::write(object,"someNumber",5000); qDebug()<<"Property value"<<object->property("someNumber").toInt(); object->setProperty("someNumber",100); return app.exec();
}
@ -
Hi,
Few problems here:
-
Use QUrl to load the file from Resource.
@
QQmlComponent component(&engine,QUrl("qrc:///main.qml"));
@ -
component.create() will return 0 if the component is not created. Don't directly go for accessing properties. Always check if it is created beforehand.
-
You cannot directly access the child item's property(property int someNumber) directly. You will need to find it first.
In your case move someNumber to ApplicationWindow.
-