[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;
    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?


  • Lifetime Qt Champion

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



  • Thanks, this was indeed the problem!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.