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. [Solved] Accessing QML children with QObject::findChild
Forum Updated to NodeBB v4.3 + New Features

[Solved] Accessing QML children with QObject::findChild

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.8k 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.
  • G Offline
    G Offline
    ggwolf
    wrote on last edited by
    #1

    Visual Studio 2010, qt5.2.1

    I have for includes:
    #include <QtQuick/QQuickView>
    #include <QtQuick/QQuickItem>

    In QML, i have a bTrying to access a child of a QML root object in C++, I have the following setup code:

    @ QQuickView *ui;
    QObject *root;
    QObject *grid;
    QObject *repeater;

    ui = new QQuickView;
    ui->setSource(QUrl::fromLocalFile("ControlPanel.qml"));
    Q_CHECK_PTR(ui);

    root = ui->rootObject();
    Q_CHECK_PTR(Root);

    grid = Root->findChild("theGrid", Qt::FindDirectChildrenOnly);
    Q_CHECK_PTR(Grid);
    @
    Visual studio has a mouseover error on the indirection operator on line 13 of the example: "Error: no instance of function template "QObject::findChild" matches the argument list." What am I doing wrong?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      findChild is a template function so you have to give the type of the child you are searching.

      In your case:

      @ grid = Root->findChild<QObject *>("theGrid", Qt::FindDirectChildrenOnly);@

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        ggwolf
        wrote on last edited by
        #3

        Thanks, this was indeed the problem!

        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