Qt World Summit: Submit your Presentation

Find QML-Child Item in C++ Code (in Widgets-App)

  • I have a problem with an Widget-App, that uses a Qml-Barcode-Scan-App.
    When I start the Qml-Barcode-Scan-App, I can find every child items:

    Client::Client(QObject* parent)
        : QObject(parent)
        , view(new QQuickView)
        , barcodeScannerViewModel(new BarcodeScannerViewModel)
        qmlRegisterType<BarcodeVideoFilter>(".....barcodevideofilter", 1, 0, "BarcodeVideoFilter");
        qmlRegisterType<BarcodeScannerViewModel>("......barcodescannerviewmodel", 1, 0, "BarcodeScannerViewModel");
        /** Set datacontext for view:
        this->view->rootContext()->setContextProperty("viewmodel", this->barcodeScannerViewModel);
        this->view->resize(800, 480);
        this->root = static_cast<QObject*>(view->rootObject());
        auto textItem = this->root->findChild<QObject*>("qmlBarcodeText");

    and so on. That all works perfect!

    But when I start the widget app and call this function to show the Barcode-Scanner, this->root can not be found and is null. Also other items can not be found by their object-name.

    What can I do to found all qml items, when my widget app is running?

    It runs on iOS 11

    Qt Creator 4.6.2
    Based on Qt 5.11.1 (Clang 8.0 (Apple), 64 bit)
    Built on Jun 18 2018 11:51:56
    From revision 1ddfb443b6

  • Hi @peter-70
    I don't understand what you mean. You are showing C++ code, the constructor of some Client class which loads the QML bar code app, etc. And you write that everything works, including the access to the child elements within QML. Isn't that C++ code part of your widget app?

  • This is definitely a confusing question. From what I'm reading it sounds like view->setSource() is failing to load the QML component. That's why view->rootObject() is returning nothing.

    Just a guess though, I'm as confused as @Diracsbracket.

Log in to reply