Modifying QML properties from C++



  • I'm trying to modify the "text" property of some QML Text widgets from C++. The QML part looks kinda like this:

    Text {
    id: my_id
    objectName: "my_id"
    text: "N/A"
    }

    And the C++ part like this:
    QObject *obj = root_object->findChild<QObject *>("my_id");
    if (obj)
    obj->setProperty("text", "new text");

    I have no problem finding the child by objectName, but setProperty() fails to update the GUI. I suspect this has something to do with dynamic properties, and I bet there is a very simple solution to this. I'd be very glad if someone could enlighten me about what I'm doing wrong here.

    BTW: If it's not possible to do this via C++, how can I access stuff via objectName from QML?

    Erlend


  • Moderators

    Hi! This should actually work. Can you please provide a minimal example with a single qml-file and a single main.cpp, so we can try to find out what the problem is?



  • Solved it: Had to use QQmlProperty text(obj, "text").write(new_text);


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.