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. Modifying QML properties from C++
Forum Updated to NodeBB v4.3 + New Features

Modifying QML properties from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 894 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.
  • E Offline
    E Offline
    Erlend E. Aasland
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Erlend E. Aasland
      wrote on last edited by
      #3

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #2

        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?

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Erlend E. Aasland
          wrote on last edited by
          #3

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

          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