Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Does QML posses this QMl type logic?



  • I am wondering is there anything in QMl that behaves like the following thing I am calling "elementX":

    elementX
    {
      id: thisElement
      listOfElementValues
      {
         value: x
         label: y
         foo: qsTr("Bar")
      }
    }
    

    such that I can now access those properties like so:

    onClicked: 
    {
      if(thisElement.foo === "Bar")
      {
        //do some logic
      }
    }
    


  • ListView
    Also, doing qsTr on the value that gets stored in foo will cause your comparison to fail unless you do:

    if(thisElement.foo === qsTr("Bar"))
    

    Or, only do qsTr on values that will be displayed and not values used in logic.



  • @fcarney I am not sure I understand how this will work. Perhaps I just don't know what I am doing. Can I pass around the id of "elementX" for instance, to a component?

    What I have is a function which I am calling. The function will create a separate component and I want to be able to pass the model into that component, for instance:

    ListModel
    {
      id: myModel
    
      ListElement
      {
        value: 3
        unit: "ft"
        minValue: 0
        maxValue: 100
        label: "Label"
      }
    }
    
    onClicked:
    {
      //the component
      showNumericKeyboard("someString", [qsTr("Cancel"), qsTr("Finished")], myModel);
    }
    

    Now, in the showNumericKeyboard component I would like to have access to: value, unit, etc... I am not sure how I can do that s/t I can use those properties of myModel like to display a value to the user or what-have-you. So in the keypad component:

    TextStyled
    {
      id: idHeaderText
      anchors.fill: parent
      style: control.style.styleTextHeader
      wrapMode: Text.WordWrap
      text: myModel.label
    }
    


  • ListView {
        width: 180; height: 500
    
        model: myModel
        delegate: Text {
            text: label + ": " + value + " " + unit
        }
    
       MouseArea {
           onClicked: {
               // stuff
           }
       }
    }
    

    The delegate is where you do something for each item in the list. Are you looking for multiple objects or just one? Maybe QtObject is what you want:

        QtObject {
            id: obj
            property int value: 3
            // other vars
        }
    


  • @fcarney Yes, that's it! That's exactly what I was looking for, thank you!


Log in to reply