Communication between Qml Objects
-
how to communicate between 2 qml objects, without defining qml object into other one.
LikeOne.qml { }
Two.qml { }
I want to send signal from one.qml to two.qml so that i can perform some actions in two.qml on the basis of some actions in one.qml.
i dont want to define one.qml in two.qml.
is there any way ? -
if you gives the qml object an Id, you can access them that way
One.qml { id: one onMySignal: two.callFunction()//for example } Two.qml { id:two }
-
@Bhushan_Sure said in Communication between Qml Objects:
is there any way ?
Yes.
One{ id:o } Two{ id:t } Component.onCompleted : { o.signalName.connect(t.functionName) }
or
One{ id:o onSignalName : { t.functionNAme()} } Two{ id:t }
-
@Bhushan_Sure
inside One.qmlsignal mySignal()
-
@Bhushan_Sure I also use this method:
Let say you wanna open other window when clicked:onClicked{
var component = Qt.createComponent("two.qml")
var window = component.createObject('oneqml mainscope id')
window.show()
} -
oneqml.qml
Rectangle{ id:one height: 400 width: 400 color: "red" signal mysignal MouseArea { anchors.fill: parent onClicked: { mysignal() } } onMysignal:{ two.bhushan() } }
twoqml.qml
Rectangle { id:two height: 400 width: 400 color: "yellow" function bhushan() { console.log("Bhushan") } }
error is two is not defined
What i have done wrong ? -
@Bhushan_Sure said in Communication between Qml Objects:
onMysignal
the reaction to mySignal needs to be inside the parent - where you instantiate One.qml object
-
@Bhushan_Sure said in Communication between Qml Objects:
But i don't want to instantiate one.qml into two.qml because
you don't have to .. see my first compment
... One{ id:o onSignalName : { t.functionNAme()} } Two{ id:t } ...
-
@Bhushan_Sure Okey you only need that function do this exactly:(Two 's 't' should be upper case be careful. You also needto recreate your two.qml file as Two.qml
//oneqml
//up the mousearea
Two{
id:caller
}onClicked{
caller.bhushan()
}