Qt World Summit: Submit your Presentation

[Solved] Accessing QML children with QObject::findChild

  • 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;

    root = ui->rootObject();

    grid = Root->findChild("theGrid", Qt::FindDirectChildrenOnly);
    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?

  • Lifetime Qt Champion


    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);@

  • Thanks, this was indeed the problem!

Log in to reply