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. QML findChild ListModel not found when instantiated in Window
Forum Updated to NodeBB v4.3 + New Features

QML findChild ListModel not found when instantiated in Window

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.5k Views 2 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.
  • hvoigtH Offline
    hvoigtH Offline
    hvoigt
    wrote on last edited by hvoigt
    #1

    Hi,

    I just discovered that when you instantiate a ListModel inside Window you can not find it via findChild from the root object. If you put it inside something else it can be found.

    Example code:

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        QList<QObject *> roots = engine.rootObjects();
        if (roots.count() < 1)
            qFatal("Error: No root QML Object found, closing Application");
    
        qDebug("root count: %i", roots.count());
    
        QObject *model = roots[0]->findChild<QObject*>("myModel");
        if (!model)
            qCritical("myModel not found");
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.3
    import QtQuick.Window 2.2
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
    
        ListModel {
            id: myModel
            objectName: "myModel"
            ListElement {
                name: "first"
            }
            ListElement {
                name: "second"
            }
        }
    
        Column {
            anchors.fill: parent
            Repeater {
                model: myModel
                delegate: Rectangle {
                    width: window.width
                    height: 15
                    color: "blue"
    
                    Text {
                        id: text
                        anchors.fill: parent
                        text: name
                    }
                }
            }
        }
    }
    

    With this code myModel is not found, but when I move it into e.g. Column findChild() finds it.

    Is this a bug?

    jpnurmiJ 1 Reply Last reply
    0
    • hvoigtH hvoigt

      Hi,

      I just discovered that when you instantiate a ListModel inside Window you can not find it via findChild from the root object. If you put it inside something else it can be found.

      Example code:

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          QList<QObject *> roots = engine.rootObjects();
          if (roots.count() < 1)
              qFatal("Error: No root QML Object found, closing Application");
      
          qDebug("root count: %i", roots.count());
      
          QObject *model = roots[0]->findChild<QObject*>("myModel");
          if (!model)
              qCritical("myModel not found");
      
          return app.exec();
      }
      

      main.qml:

      import QtQuick 2.3
      import QtQuick.Window 2.2
      
      Window {
          id: window
          visible: true
          width: 640
          height: 480
      
          ListModel {
              id: myModel
              objectName: "myModel"
              ListElement {
                  name: "first"
              }
              ListElement {
                  name: "second"
              }
          }
      
          Column {
              anchors.fill: parent
              Repeater {
                  model: myModel
                  delegate: Rectangle {
                      width: window.width
                      height: 15
                      color: "blue"
      
                      Text {
                          id: text
                          anchors.fill: parent
                          text: name
                      }
                  }
              }
          }
      }
      

      With this code myModel is not found, but when I move it into e.g. Column findChild() finds it.

      Is this a bug?

      jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      Is this a bug?

      Yes, I think this could be fairly well considered as a bug. It should be easy to fix by setting QQuickWindow as a QObject-parent of QQuickRootItem in http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/quick/items/qquickwindow.cpp#n520. Not 100% sure if it has any undesired side-effects, though.

      1 Reply Last reply
      0
      • hvoigtH Offline
        hvoigtH Offline
        hvoigt
        wrote on last edited by
        #3

        Solution: Filed this as a bugreport here: https://bugreports.qt.io/browse/QTBUG-55592

        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