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 NULL looking for QML objects
Forum Updated to NodeBB v4.3 + New Features

findChild always returns NULL looking for QML objects

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 3.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.
  • P Offline
    P Offline
    poncho524
    wrote on last edited by
    #1

    Re: [solved] QObject::findChild() not working for QML Items inside Window

    I have a QQuickWindow, created a QQmlComponent and setParentItem(window->contentItem)

    I'm trying to find one of the QQuickItem's now existing in that window, but
    window->findChild<QQuickItem*> returns nothing

    neither does
    window->contentItem->findChild

    What gives? I'm trying to find it by its objectName... this should work, no?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      poncho524
      wrote on last edited by
      #2

      Ok, here's the screwy behavior i found

      QQmlEngine engine;
      
      QQmlComponent formWindowComp(&engine, QUrl("qrc:/formWindow.qml"));
      QQuickWindow *formWindow=qobject_cast<QQuickWindow*>(formWindowComp.create());
      
      QQmlComponent menuComp(engine, QUrl("qrc:/testqml.qml"));
      QQuickItem *menu=qobject_cast<QQuickItem*>(menuComp.create());
      menu->setParentItem(formWindow->contentItem());
      

      all works find here... i get testqml.qml showing up in my formwindow.qml.

      Now when i want to find an element inside of testqml.qml, you'd think i could do

      QObject *thing=formWindow->findChild<QObject*>("thing");
      

      but this always returns NULL

      This is what works tho...

      QObject *thing;
      auto list=formWindow->contentItem->childItems();
      for (auto i : list) {
        thing=i->findChild<QObject*>("thing");
        if (thing) break;
      }
      

      So why doesn't "thing" exist as a child of formWindow? or why doesn't it exist as a child of formwindow->contentItem?

      Why do i have to get the children of contentItem before i can successfully search?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stevemcgf
        wrote on last edited by
        #3

        parent and parentItem are different concepts parentItem defines the "visual" parent and parent defines the QObject parent.

        To get the visual childs you should use childItems.

        Take a look at this link
        http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html

        Z 1 Reply Last reply
        0
        • S stevemcgf

          parent and parentItem are different concepts parentItem defines the "visual" parent and parent defines the QObject parent.

          To get the visual childs you should use childItems.

          Take a look at this link
          http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html

          Z Offline
          Z Offline
          zilti
          wrote on last edited by
          #4
          This post is deleted!
          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