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. How to communicate between two different QML components
Qt 6.11 is out! See what's new in the release blog

How to communicate between two different QML components

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

    I have two qml files i.e. main.qml & myComponent.qml

    The main.qml invoke myComponent.qml like below:-

    onTriggered: {
        var component = Qt.createComponent("myComponent.qml")
        if (component.status === Component.Ready) {
            .
            .
            .
            var window = component.createObject(root)
    
            if (window !== null){
                window.show()
            } 
            else {
                console.error("Error creating window object")
            }
        } else {
            console.error("Error loading component:", component.errorString())
        }
    }
    

    I've also defined Connection within the main.qml file to receive latest data from c++ like below:-

    Connections {
        target: myObject  // C++ object exposed to qml
        function onSignalRecv(response) {
            console.log(response)  // Here i'm getting the latest data
        }
    }
    

    Now i want to transfer this latest data from main.qml to myComponent.qml. Every time I receive data in onSignalRecv(..) i want to transfer it.

    The myComponent.qml contains a ListView to show updated data and i want to update the data in realtime.

    johngodJ 1 Reply Last reply
    0
    • J johndummy

      I have two qml files i.e. main.qml & myComponent.qml

      The main.qml invoke myComponent.qml like below:-

      onTriggered: {
          var component = Qt.createComponent("myComponent.qml")
          if (component.status === Component.Ready) {
              .
              .
              .
              var window = component.createObject(root)
      
              if (window !== null){
                  window.show()
              } 
              else {
                  console.error("Error creating window object")
              }
          } else {
              console.error("Error loading component:", component.errorString())
          }
      }
      

      I've also defined Connection within the main.qml file to receive latest data from c++ like below:-

      Connections {
          target: myObject  // C++ object exposed to qml
          function onSignalRecv(response) {
              console.log(response)  // Here i'm getting the latest data
          }
      }
      

      Now i want to transfer this latest data from main.qml to myComponent.qml. Every time I receive data in onSignalRecv(..) i want to transfer it.

      The myComponent.qml contains a ListView to show updated data and i want to update the data in realtime.

      johngodJ Offline
      johngodJ Offline
      johngod
      wrote on last edited by
      #2

      @johndummy Hi

      define a signal in myComponent.qml

      signal updateMyData(var newData)//adapt to your data type
      

      then in main.qml after the creation of the myComponent add
      ```
      onSignalRecv.connect( window.onSignalRecv ) // dont put arguments here

      Then update your list in the updateMyData signal handler
      J 1 Reply Last reply
      0
      • johngodJ johngod

        @johndummy Hi

        define a signal in myComponent.qml

        signal updateMyData(var newData)//adapt to your data type
        

        then in main.qml after the creation of the myComponent add
        ```
        onSignalRecv.connect( window.onSignalRecv ) // dont put arguments here

        Then update your list in the updateMyData signal handler
        J Offline
        J Offline
        johndummy
        wrote on last edited by
        #3
        This post is deleted!
        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