Reputation: 407
i got two Windows
inside the same .qml file.
Window1 has a textinput1
and a button
, and when I press the button
, i'd like to send the string value from that textinput
to the Window2
textInput2
.
I'm new to Qt and QML, been reading a lot on signals
, Loaders
, properties
and can't materialize this kind of transfer. Can anyone do a simple 10-line example of such transfer please?
Window {
id:window
TextInput {
id:text1
text: "This value is needed in the second Window!"
}
Button {
onClicked: window2.open()
}
Window {
id.window2
function open(){
visible = true
}
Text {
text: text1.text
}
}
}
If I do this it gives me ReferenceError: text1 is not defined
, how can I reference the text1
from the first Window
?
Upvotes: 0
Views: 1385
Reputation: 12864
I would prefer to use signals in such case:
Window {
id: window1
title: "window 1"
visible: true
width: 600
height: 600
signal someSignal()
Button {
anchors.centerIn: parent
text: "Open window"
onClicked: window1.someSignal()
}
Window {
id: window2
title: "window 2"
width: 400
height: 400
// you can use this instead of Connections
//Component.onCompleted: {
// window1.someSignal.connect(show);
//}
}
Connections {
target: window1
onSomeSignal: {
window2.show();
}
}
}
I think this is more ... how do you say? ... more imperative -)
Upvotes: 1
Reputation: 49289
i got two Windows inside the same .qml file.
If you did then your code will work. Since it doesn't work, I will assume each window is defined in its own qml file, and you only instantiate the two windows in the same qml file.
If I do this it gives me ReferenceError: text1 is not defined, how can I reference the text1 from the first Window?
You will have to be able to reference the window first, and it should provide an interface to reference the text.
Keep in mind that ideally id
s should only be used to reference stuff in the same source. On rare occasions you could go further, and reference id
s down the object tree, but only parents, and none of their out-of-line children, it will however work for in-line children that are given id
s in that same source. Meaning that if window2
is created inside window
then you will be able to reference window
from inside window2
. But if both windows are siblings in another object, the id
won't resolve.
Obj1
Obj2
Obj4
Obj3
In this example object tree, Obj1
will resolve from any of the objects. However, Obj3
will not be able to resolve Obj2
if the id
is given inside Obj2
, but will resolve if the id
for Obj2
is given inside Obj1
. But there is no way to resolve Obj4
from Obj3
. because the id
doesn't act like a property, you can't do someId.someOtherId
, that's only possible for properties. You cannot do somePropertyObject.someId
neither. You can only begin with either an id
or a property, and continue only with sub-properties.
When the id
is not applicable, can expose objects or properties either as properties or property aliases. The first is useful when you want to expose a whole object, the second when you want to expose a particular property of an object:
Item {
property Item innerItem: inner // gives you access to the "entire" inner object
property alias innerWidth: inner.width // gives you access to a property of inner
Item {
id: inner
}
}
You can also have aliases to aliases.
Upvotes: 1