Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Not able to access members in QML File
Forum Updated to NodeBB v4.3 + New Features

Not able to access members in QML File

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 766 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    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"<&lt;object-&gt;property("someNumber").toInt();
    
    object->setProperty("someNumber",100);
    
    return app.exec&#40;&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      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.

      157

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved