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 Updated to NodeBB v4.3 + New Features

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 1 Watching
  • 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 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
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      0
      • JoeCFDJ JoeCFD

        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 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.HilkJ 1 Reply Last reply
        0
        • T tubbadu

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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
          0
          • J.HilkJ J.Hilk

            @tubbadu
            have you tried:

            property var savedObj
            
            T Offline
            T Offline
            tubbadu
            wrote on last edited by
            #5

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

            J.HilkJ 1 Reply Last reply
            0
            • T tubbadu

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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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 last edited by
                #7

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

                1 Reply Last reply
                1

                • Login

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