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 can I make a connection between 2 qml objects in differents screens?

How can I make a connection between 2 qml objects in differents screens?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlconnectionsignalconnect
3 Posts 3 Posters 636 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.
  • M Offline
    M Offline
    Mateus
    wrote on last edited by
    #1

    I'm doing a simple example to learn how do use Signals and Connections in qml.

    Notifier.qml:

    import QtQuick
    
    Item {
    
        property alias rectColor: notifierRectId.color
        width: notifierRectId.width
        height: notifierRectId.height
        property int count: 0
        signal notify( string count)//Declare signal
        property Receiver target : null
    
        onTargetChanged: {
            notify.connect(target.receiveInfo)
        }
    
    
        Rectangle {
            id : notifierRectId
            width: 200
            height: 200
            color: "red"
    
    
            Text {
                id : displayTextId
                anchors.centerIn: parent
                font.pointSize: 20
                text : count
            }
    
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    count++
                    notify(count)
                }
            }
        }
    
    }
    
    

    Receiver.qml:

    import QtQuick
    
    Item {
    
        property alias rectColor: receiverRectId.color
        width: receiverRectId.width
        height: receiverRectId.height
    
        function receiveInfo( count){
            receiverDisplayTextId.text = count
            console.log("Receiver received number : "+ count)
        }
    
        Rectangle {
            id : receiverRectId
            width: 200
            height: 200
            color: "red"
    
    
            Text {
                id : receiverDisplayTextId
                anchors.centerIn: parent
                font.pointSize: 20
                text : "0"
            }
        }
    
    }
    

    And now I have one main.qml that instance these 2 QML objects:

    import QtQuick
    import QtQuick.Window
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("External Components with signals and slots")
    
        Notifier{
            id : notifierId
            rectColor: "yellowgreen"
            target: receiverId
            visible: true
        }
    
        Receiver {
            id : receiverId
            rectColor: "dodgerblue"
            anchors.right: parent.right
            visible: right
        }
    
        Component.onCompleted: {
            notifierId.notify.connect(receiverId.receiveInfo)//Connect signal to slot
        }
    }
    

    My question is, how can I instantiate Receiver in another .qml and keep the connection working? I need to send data between 2 different screens. I've tried several alternatives, but it always gives error.

    Thanks!

    F 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You can have another connect statement like thin main.qml
      receiverId.notify.connect(notifierId.receiveInfo)

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • M Mateus

        I'm doing a simple example to learn how do use Signals and Connections in qml.

        Notifier.qml:

        import QtQuick
        
        Item {
        
            property alias rectColor: notifierRectId.color
            width: notifierRectId.width
            height: notifierRectId.height
            property int count: 0
            signal notify( string count)//Declare signal
            property Receiver target : null
        
            onTargetChanged: {
                notify.connect(target.receiveInfo)
            }
        
        
            Rectangle {
                id : notifierRectId
                width: 200
                height: 200
                color: "red"
        
        
                Text {
                    id : displayTextId
                    anchors.centerIn: parent
                    font.pointSize: 20
                    text : count
                }
        
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        count++
                        notify(count)
                    }
                }
            }
        
        }
        
        

        Receiver.qml:

        import QtQuick
        
        Item {
        
            property alias rectColor: receiverRectId.color
            width: receiverRectId.width
            height: receiverRectId.height
        
            function receiveInfo( count){
                receiverDisplayTextId.text = count
                console.log("Receiver received number : "+ count)
            }
        
            Rectangle {
                id : receiverRectId
                width: 200
                height: 200
                color: "red"
        
        
                Text {
                    id : receiverDisplayTextId
                    anchors.centerIn: parent
                    font.pointSize: 20
                    text : "0"
                }
            }
        
        }
        

        And now I have one main.qml that instance these 2 QML objects:

        import QtQuick
        import QtQuick.Window
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("External Components with signals and slots")
        
            Notifier{
                id : notifierId
                rectColor: "yellowgreen"
                target: receiverId
                visible: true
            }
        
            Receiver {
                id : receiverId
                rectColor: "dodgerblue"
                anchors.right: parent.right
                visible: right
            }
        
            Component.onCompleted: {
                notifierId.notify.connect(receiverId.receiveInfo)//Connect signal to slot
            }
        }
        

        My question is, how can I instantiate Receiver in another .qml and keep the connection working? I need to send data between 2 different screens. I've tried several alternatives, but it always gives error.

        Thanks!

        F Offline
        F Offline
        freedbrt
        wrote on last edited by freedbrt
        #3

        There are many ways to do this, one of is:

        Notifier{
              id : notifierId
              rectColor: "yellowgreen"
              target: receiverId
              visible: true
              onNotify: receiverId.receiveInfo(param)
          }
        
          Receiver {
              id : receiverId
              rectColor: "dodgerblue"
              anchors.right: parent.right
              visible: right
          }
        
        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