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. setProperty value by object and propertyname

setProperty value by object and propertyname

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.5k Views 3 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
    themts
    wrote on last edited by
    #1

    Hey guys,

    I have the following problem:
    I want to create a textEdit-component which implements a two-way binding.

    Lets say I have a c++ object with several Q_PROPERTYs.
    Now I need a textEdit for each of this properties with a two way binding.
    Normally I can do something like this for each text Element:

    1. 
    TextInput: {
        id: t1
        ...
        onTextChanged: myObject.setText1(text)
    }
    
    Connections {
        target: myObject
        onTextChanged1: t1.text = myObject.text1
    }
    

    My idea is to pass an object and a property name to a custom TextInput.
    So I want to do all the other handling inside my custom component.

    CustomComponent {
        target: targetObject
        property: text1
    }
    

    How can I set a property by name and object in qml?
    Is there a function like setProperty(object, "property") within qml?

    CU
    mts

    jeremy_kJ 1 Reply Last reply
    0
    • T themts

      Hey guys,

      I have the following problem:
      I want to create a textEdit-component which implements a two-way binding.

      Lets say I have a c++ object with several Q_PROPERTYs.
      Now I need a textEdit for each of this properties with a two way binding.
      Normally I can do something like this for each text Element:

      1. 
      TextInput: {
          id: t1
          ...
          onTextChanged: myObject.setText1(text)
      }
      
      Connections {
          target: myObject
          onTextChanged1: t1.text = myObject.text1
      }
      

      My idea is to pass an object and a property name to a custom TextInput.
      So I want to do all the other handling inside my custom component.

      CustomComponent {
          target: targetObject
          property: text1
      }
      

      How can I set a property by name and object in qml?
      Is there a function like setProperty(object, "property") within qml?

      CU
      mts

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      @themts said in setProperty value by object and propertyname:

      How can I set a property by name and object in qml?
      Is there a function like setProperty(object, "property") within qml?

      object[property] = value works, where property is a string representation of the property name.
      eg: t1["text"] = myObject.text1

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by p3c0
        #3

        @themts

        Is there a function like setProperty(object, "property") within qml?

        AFAIK no such way in QML. But if CustomComponent is a registered C++ class then you can use setProperty function for that object.

        157

        1 Reply Last reply
        0
        • T Offline
          T Offline
          themts
          wrote on last edited by
          #4

          Hi

          I could find a solution that is working for me quite well:

              property alias target : targetBinding.target
              property alias property : targetBinding.property
          
              Binding {
                  id: targetBinding
              }
          
              Binding {
                  id: sourceBinding
                  target: item1
                  property: "text"
                  value: (item1.property != "") && (item1.target != undefined) ? item1.target[item1.property] : ""
              }
          
              onEditingFinished: {
                  if (property == "")
                      return;
          
                  targetBinding.value = text;
              }
          

          It would be nice if qt will get a built-in two way binding in future (see wpf).

          CU

          jeremy_kJ 1 Reply Last reply
          0
          • T themts

            Hi

            I could find a solution that is working for me quite well:

                property alias target : targetBinding.target
                property alias property : targetBinding.property
            
                Binding {
                    id: targetBinding
                }
            
                Binding {
                    id: sourceBinding
                    target: item1
                    property: "text"
                    value: (item1.property != "") && (item1.target != undefined) ? item1.target[item1.property] : ""
                }
            
                onEditingFinished: {
                    if (property == "")
                        return;
            
                    targetBinding.value = text;
                }
            

            It would be nice if qt will get a built-in two way binding in future (see wpf).

            CU

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            @themts said in setProperty value by object and propertyname:

            It would be nice if qt will get a built-in two way binding in future (see wpf).

            You're not alone in this wish.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            0

            • Login

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