Not able to access members in QML File

  • Not able to set, read using QQmlProperty
    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();
    qDebug()<<"Property value"<&lt;object-&gt;property("someNumber").toInt();
    return app.exec&#40;&#41;;


  • Moderators


    Few problems here:

    1. Use QUrl to load the file from Resource.
      QQmlComponent component(&engine,QUrl("qrc:///main.qml"));

    2. 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.

    3. 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.

Log in to reply