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. QML and C++ Signal Interface w/ Dynamically Loaded QML Objects
Forum Updated to NodeBB v4.3 + New Features

QML and C++ Signal Interface w/ Dynamically Loaded QML Objects

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 514 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.
  • K Offline
    K Offline
    kengineer
    wrote on last edited by
    #1

    So I have an application in which I am using QML to provide the UI and visual elements and C++ to provide some more complex back-end functionality of the app. I am trying to figure out how to interface the two.

    Following this guide:
    http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

    An example is using QML signals to trigger a C++ slot. This works nicely if you have a simply static root hierarchical QML object structure - like their simple example where a you simply do a root object lookup of a QML view for the file that was loaded.

    However, in my case, while using a loader or similar to dynamically load a QML page at runtime, how would I reference signals from those "deeper" dynamic sub-objects in C++?

    Here is a simple example of my use case to help clarify the issue:

    // main.cpp
    QQuickView view(QUrl(QLatin1String("qrc:/main_page.qml")));
    // How can we access signals or objects in the
    // "some_other_page.qml" loaded instance?
    
    
    
    // main_page.qml
    Item {
        Loader {
            id: myLoader
            source: "some_other_page.qml"
            active: true
        }
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kengineer
      wrote on last edited by
      #2

      Ok I figured out a way to do this in the case of a Loader, looking at our page with the loader we can reference the "sub signal" and connect it a signal in the local page. This upper level signal can then be connected to the slot in C++ code.

      // main_page.qml
      
      signal qmlSignal(string msg) 
      
      Item {
          Loader {
              id: myLoader
              source: "some_other_page.qml"
              active: true
                  onLoaded: {
                      item.anotherqmlSignal.connect(qmlSignal)
                  }
          }
      }
      
      

      Another way to make a connection but maybe more crude is to use a "Connection" type and then call the sub-signal from the handler.

      Not sure if this is the best way but this seems to work.

      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