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. Get data of the ListElement, which is currently bound to the TableView
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 650 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.
  • XequtorX Offline
    XequtorX Offline
    Xequtor
    wrote on last edited by
    #1

    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
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      2
      • XequtorX Offline
        XequtorX Offline
        Xequtor
        wrote on last edited by
        #3

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

        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