How to assign an object id to a variable in a QML script
-
wrote on 24 Jun 2022, 14:38 last edited by
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!
-
wrote on 24 Jun 2022, 15:30 last edited by JoeCFD
I do not think it will work since savedObj is a string, not an object anymore.
function doThings(){
t1.text = "hello";
} -
I do not think it will work since savedObj is a string, not an object anymore.
function doThings(){
t1.text = "hello";
}wrote on 29 Jun 2022, 12:01 last edited by@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
have you tried:property var savedObj
-
wrote on 30 Jun 2022, 08:48 last edited by
@J-Hilk Thanks for the reply! I tried, but the assignation
savedObj = id
fails anyway :( -
@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 } } }
-
wrote on 30 Jun 2022, 09:01 last edited by
I was trying to assign
id
instead oflbl1
, that's the problem, thanks!
1/7