How can I make a connection between 2 qml objects in differents screens?
Unsolved
QML and Qt Quick
-
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!
-
You can have another connect statement like thin main.qml
receiverId.notify.connect(notifierId.receiveInfo) -
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 }