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. findChild() always returns 0 for QML objects
QtWS25 Last Chance

findChild() always returns 0 for QML objects

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlpropertiesfindchild
3 Posts 2 Posters 6.3k Views
  • 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 19 Mar 2018, 09:16 last edited by Erlend E. Aasland
    #1

    Hi,

    I've got a QML component embedded in a Qt C++ app via QQuickView. Everything loads nice and neat, but now I need to control the QML module by modifying its properties. I've tried a bunch of findChild<QObject *>(''tag''), but I always get null. I've got objectName: ''tag'' set in the QML component, so I assume the tag is set properly. Current code:

    form.cpp:

    QObject *obj = this->findChild<QObject *>("tag");
    if (obj) {
        obj->setProperty("ticks", tick_count);
    } else {
        qDebug() << "Could not find QML component!";
    }
    

    component.qml:

    Item {
        id: tag
        objectName: "tag"
        property int ticks: 0
        /* … */
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 19 Mar 2018, 10:53 last edited by
      #2

      @Erlend-E.-Aasland said in findChild() always returns 0 for QML objects:

      this->findChild<QObject *>("tag");

      What is the this object? Is it the QML engine or something that actually holds your QML components?

      objectName: "tag"

      That'c correct. findChild<>() looks for named objects.

      component.qml:

      Are you sure that component is instantiated? Is there only one copy of it?

      In general, I'd rather recommend a different approach. Instead of forcing properties like this, use the Connections element. Add some QObject "controller" which will manage C++ - QML communiacation, add it as engine context property, then refer to it in your QML code. Either like this or like this.

      (Z(:^

      E 1 Reply Last reply 19 Mar 2018, 15:11
      2
      • S sierdzio
        19 Mar 2018, 10:53

        @Erlend-E.-Aasland said in findChild() always returns 0 for QML objects:

        this->findChild<QObject *>("tag");

        What is the this object? Is it the QML engine or something that actually holds your QML components?

        objectName: "tag"

        That'c correct. findChild<>() looks for named objects.

        component.qml:

        Are you sure that component is instantiated? Is there only one copy of it?

        In general, I'd rather recommend a different approach. Instead of forcing properties like this, use the Connections element. Add some QObject "controller" which will manage C++ - QML communiacation, add it as engine context property, then refer to it in your QML code. Either like this or like this.

        E Offline
        E Offline
        Erlend E. Aasland
        wrote on 19 Mar 2018, 15:11 last edited by
        #3

        @sierdzio Thanks for your input! The this-object was the parent widget holding the QQuickView. I solved it by saving the root object of the QQuickView:

        QObject *obj = quickview->rootObject();
        if (obj)
            obj->setProperty("ticks", tick_count);
        else
            qDebug() << "Could not set tick count!";
        

        Anyway, I'll have a look at the different approach you suggest. It sound like a more robust way of interacting with QML.

        Best regards, E

        1 Reply Last reply
        0

        1/3

        19 Mar 2018, 09:16

        • Login

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