Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to assign an object id to a variable in a QML script
Forum Update on Monday, May 27th 2025

How to assign an object id to a variable in a QML script

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tubbadu
    wrote on 24 Jun 2022, 14:38 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 24 Jun 2022, 15:30 last edited by JoeCFD
      #2

      I do not think it will work since savedObj is a string, not an object anymore.

      function doThings(){
      t1.text = "hello";
      }

      T 1 Reply Last reply 29 Jun 2022, 12:01
      0
      • J JoeCFD
        24 Jun 2022, 15:30

        I do not think it will work since savedObj is a string, not an object anymore.

        function doThings(){
        t1.text = "hello";
        }

        T Offline
        T Offline
        tubbadu
        wrote on 29 Jun 2022, 12:01 last edited by
        #3

        @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

        J 1 Reply Last reply 29 Jun 2022, 12:16
        0
        • T tubbadu
          29 Jun 2022, 12:01

          @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

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 29 Jun 2022, 12:16 last edited by
          #4

          @tubbadu
          have you tried:

          property var savedObj
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          T 1 Reply Last reply 30 Jun 2022, 08:48
          0
          • J J.Hilk
            29 Jun 2022, 12:16

            @tubbadu
            have you tried:

            property var savedObj
            
            T Offline
            T Offline
            tubbadu
            wrote on 30 Jun 2022, 08:48 last edited by
            #5

            @J-Hilk Thanks for the reply! I tried, but the assignation savedObj = id fails anyway :(

            J 1 Reply Last reply 30 Jun 2022, 08:55
            0
            • T tubbadu
              30 Jun 2022, 08:48

              @J-Hilk Thanks for the reply! I tried, but the assignation savedObj = id fails anyway :(

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 30 Jun 2022, 08:55 last edited by
              #6

              @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
                      }
                  }
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tubbadu
                wrote on 30 Jun 2022, 09:01 last edited by
                #7

                I was trying to assign id instead of lbl1, that's the problem, thanks!

                1 Reply Last reply
                1

                1/7

                24 Jun 2022, 14:38

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved