Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Get data of the ListElement, which is currently bound to the TableView

    QML and Qt Quick
    2
    3
    464
    Loading More Posts
    • 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.
    • Xequtor
      Xequtor last edited by

      Hello All,

      I am trying to achieve the following: Add a table, which will have one column with delegates (TextFields, SpinBoxes, etc) and the rest of the columns will be just texts, taken from ListModel. What control will be added on the current row depends on one of the properties in the ListModel. I am adding the controls by a Loader and I have a script, which returns a delegate of the respective control. In order to decide which element to put in the current row, I need to know the value of the ListElement's property in the current row. This is my code:

       Item {
                  id: propertiesTableContainer
                  anchors.top: propertiesFilter.bottom
                  anchors.topMargin: 10
                  anchors.bottom: parent.bottom
                  width: parent.width
      
                  Item {
                      id: propertiesDelegatesContainer
      
                      Component {
                          id: propertiesTextBoxDelegate
                          TextField {}
                      }
      
                      Component {
                          id: propertiesSpinBoxDelegate
                          SpinBox {
                              stepSize: lStepSize
                              decimals: lDecimalCounts
                          }
                      }
      
                      Component {
                          id: propertiesCheckboxDelegate
                          CheckBox {}
                      }
                  }
      
                  ListModel {
                      id: propertiesModel
      
                      ListElement {
                           name: "gAudioSongTitle"
                           value: ""
                           description: "The title of currently playing song"
                           type: "String"
                           range: ""
                      }
      
                      ListElement {
                           name: "gAudioSongTotalTime"
                           value: ""
                           description: "The title of currently playing song"
                           type: "Int"
                           range: ""
                      }
      
                      ListElement {
                           name: "gAudioSongTotalTime"
                           value: ""
                           description: "The title of currently playing song"
                           type: "Bool"
                           range: ""
                      }
      
                      ListElement {
                           name: "gAudioSongTotalTime"
                           value: ""
                           description: "The title of currently playing song"
                           type: "float"
                           range: ""
                      }
                  }
      
                  TableView {
                      id: propertiesTable
                      anchors.top: parent.top
                      anchors.bottom: parent.bottom
                      model: propertiesModel
                      width:parent.width
      
                      TableViewColumn {
                          id: propertiesColumnName
                          role: "name"
                          title: "Name"
                          elideMode: Text.ElideRight
                          width: 100
                      }
                      TableViewColumn {
                          id: propertiesColumnValue
                          role: "value"
                          title: "Value"
                          width: 200
      
                          delegate: Loader {
                              anchors.fill: parent
                              property real lStepSize: 0.1
                              property int lDecimalCounts: 1
      
                              sourceComponent: {
                                  switch(propertiesModel.get(0).type)
                                  {
                                      case "String":
                                      case "string":
                                          console.log(lPropertiesTableCurrentRow);
                                          return propertiesTextBoxDelegate;
                                      case "Int":
                                      case "int":
                                      case "Int32":
                                      case "int32":
                                          console.log(lPropertiesTableCurrentRow);
                                          lStepSize = 1.0;
                                          lDecimalCounts = 0;
                                          return propertiesSpinBoxDelegate;
                                      case "Float":
                                      case "float":
                                      case "Double":
                                      case "double":
                                      case "Real":
                                      case "real":
                                          console.log(lPropertiesTableCurrentRow);
                                          lStepSize = 0.1;
                                          lDecimalCounts = 1;
                                          return propertiesSpinBoxDelegate;
                                      case "Boolean":
                                      case "Bool":
                                      case "bool":
                                          console.log(lPropertiesTableCurrentRow);
                                          return propertiesCheckboxDelegate
                                      default:
                                          console.log(lPropertiesTableCurrentRow);
                                          return propertiesTextBoxDelegate;
                                  }
                              }
                          }
                      }
                      TableViewColumn {
                          id: propertiesColumnDescription
                          role: "description"
                          title: "Description"
                          elideMode: Text.ElideRight
                          width: 200
                      }
                      TableViewColumn {
                          id: propertiesColumnType
                          role: "type"
                          title: "Type"
                          elideMode: Text.ElideRight
                          width: 100
      
                      }
                      TableViewColumn {
                          id: propertiesColumnRange
                          role: "range"
                          title: "Range"
                          elideMode: Text.ElideRight
                          width: 100
                      }
                  }
      
              }
      

      As you can see I am trying to get the value by this expression:

      propertiesModel.get(0).type
      

      I need either to replace the 0 with the index of the currently processed ListElement or find another way to get the data from the type column. I tried to use on global property, which starts at 0 and I increase it by one everytime a delegate is loaded, but then I've got Binding Loop error. It was like this:

      propertiesModel.get(globalProperty++).type
      

      So, is it possible to achieve what am I trying to, or should I try to find another way (without using TableView)

      Thanks,

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        Make switch statement like this. It should solve your problem.

        switch(propertiesModel.get(styleData.row).type)

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply Reply Quote 2
        • Xequtor
          Xequtor last edited by

          A huuuuuge thanks, @dheerendra . You really saved my day :)

          1 Reply Last reply Reply Quote 0
          • First post
            Last post