TableView rowDelegate - Getting values of column prior to commiting to table



  • How to get a handle to values of TableView before they are commited to table. Below is a clip of my code, in the row delegate I have a Save button and when clicked I would like to get a handle to all the fields of the row delegate. (there are 4) . If anyone has a clue please scroll down to the save button below and look at the onClicked Method.

    Thank you very much for any info you can provide.

     TableView {
            id:serverTable
            anchors.margins:15
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: titleArea.bottom
            anchors.topMargin: fm.height*2
            anchors.bottom: serverButtonArea.top
            anchors.bottomMargin: 15
            alternatingRowColors: true
            selectionMode: SelectionMode.SingleSelection
            model: serverListModel;
            onCurrentRowChanged : {
                console.log("CURRENT ROW CHANGED");
            }
            rowDelegate: Rectangle {
                height: styleData.selected ? fm.height*1.8 : fm.height*1.3
                Behavior on height{ NumberAnimation{} }
                color:   styleData.selected ? "lightsteelblue" :  (styleData.alternate? "white" : "#DDE8FF")
                RowLayout {
                    id: buttons
                    x: nameCol.width + ipCol.width + portCol.width + maxRetriesCol.width + 20
                    anchors.verticalCenter: parent.verticalCenter
                    visible:  styleData.selected ? true : false
                    spacing: 10
                    Button {
                        id: saveB
                        text: "Save"
                        style : ButtonStyle {
                            background: Rectangle {
                                implicitWidth: 60
                                implicitHeight: fm.height*1.2
                                border.width: control.activeFocus ? 2 : 1
                                border.color: "#888"
                                radius: 5
                                gradient: Gradient {
                                    GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                                    GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                                }
                            }
                        }
                        onClicked: {
                            console.log("Row = " + styleData.row);
                            //console.log("What goes here to get contents of cell  = " + nameCol.);      // TRYING TO GET CONTENTS OF COL 0
                        }
                    }
                    Button {
                        id: canceB
                        text: "Cancel"
                        style : ButtonStyle {
                            background: Rectangle {
                                implicitWidth: 60
                                implicitHeight: 22
                                border.width: control.activeFocus ? 2 : 1
                                border.color: "#888"
                                radius: 4
                                gradient: Gradient {
                                    GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                                    GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                                }
                            }
                        }
                    }
    
                }
            }
            TableViewColumn {
                id: nameCol
                role: "name"
                title: "Name"
                width: 100
            }
            TableViewColumn {
                id: ipCol
                role: "ip"
                title: "IP Address"
                width: 200
            }
            TableViewColumn {
                id : portCol
                role: "port"
                title: "Port"
                width: 80
            }
            TableViewColumn {
                id: maxRetriesCol
                role: "maxRetries"
                title: "Max Retries"
                width: 100
            }
            itemDelegate: {
                return editableDelegate;
            }
    

    Edited: Please use ``` for posting code blocks - p3c0


  • Moderators

    Hi @DBoosalis
    If serverListModel is a C++ model better to do it on model side. You can write a Q_INVOKABLE function in model to grab the data and call it from QML. If it is a ListModel you can iterate the model and use get to fetch data. Eg:

    tableView.model.get(0).title
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.