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. Find QML-Child Item in C++ Code (in Widgets-App)
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 541 Views
  • 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.
  • P Offline
    P Offline
    peter-70
    wrote on 16 Jul 2018, 15:38 last edited by
    #1

    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->setResizeMode(QQuickView::SizeRootObjectToView);
        this->view->setSource(QUrl("qrc:/Resources/barcodescanner.qml"));
        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

    D 1 Reply Last reply 29 Jul 2018, 06:45
    0
    • P peter-70
      16 Jul 2018, 15:38

      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->setResizeMode(QQuickView::SizeRootObjectToView);
          this->view->setSource(QUrl("qrc:/Resources/barcodescanner.qml"));
          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

      D Offline
      D Offline
      Diracsbracket
      wrote on 29 Jul 2018, 06:45 last edited by
      #2

      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?

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ambershark
        wrote on 30 Jul 2018, 00:08 last edited by
        #3

        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.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        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