How to assign an object id to a variable in a QML script
-
Hello there! This is my first post here, I hope to do nothing wrong
I'm developing a plasmoid in QML, and I'm trying to store an object ID into a variable, so that later I'll be able to edit its properties, something like this:
property string savedObj //string? TextEdit { id: t1 onFocusChange { if (focus) { savedObj = id //or something like this } } } ... function doThings(){ savedObj.text = "hello"; }
is it possible to do something like this? I need to do this because I have a lot of TextEdit and I need to know the last one that got focused
thanks in advance!
-
I do not think it will work since savedObj is a string, not an object anymore.
function doThings(){
t1.text = "hello";
}@JoeCFD thanks for your answer! Unfortunately I need that function to interact with different objects (the last one that got focus for istance) so I have t1, t2, t3 and whenever they get focus they update the savedObj variables (I declared it as string because I don't know what else to declare), and when I call doThings() it should change the text to the last TextEdit who got focused
-
@JoeCFD thanks for your answer! Unfortunately I need that function to interact with different objects (the last one that got focus for istance) so I have t1, t2, t3 and whenever they get focus they update the savedObj variables (I declared it as string because I don't know what else to declare), and when I call doThings() it should change the text to the last TextEdit who got focused
-
@tubbadu nope, works like a charm:
Window { visible: true width: 500 height: 500 title: qsTr("Hello World2") Column { anchors.fill: parent Label{ id: lbl1 text: "Label1" } Label{ id:lbl2 text: "Label2" } } property var selectedLabel: lbl1 property int index: 2 Timer { running: true repeat: true interval: 1000 onTriggered: { index++ selectedLabel.text = "Label" + index selectedLabel == lbl1 ? selectedLabel = lbl2 : selectedLabel = lbl1 } } }