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

TableView how to delete a row



  • Hello to all. I have been using Qt/QtQuick for a project recently and I am stuck on deleting rows from a TableView. Below is the offending code

     TableView {
                    anchors.top: aBar.bottom
                    anchors.topMargin: 3
                    sortIndicatorVisible: true
                    frameVisible: false
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    onSortIndicatorColumnChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
                    onSortIndicatorOrderChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
                    id: dataView
    
                    TableViewColumn {
                        role: "Nome"
                        title: "Nome"
                        width: 200
                    }
    
                    TableViewColumn {
                        role: "Residenza"
                        title: "Residenza"
                        width: 200
                    }
    
                    TableViewColumn {
                        role: "Assicurazione"
                        title: "Assicurazione"
                        width: 200
                    }
    
                    TableViewColumn {
                        width: 128
                        resizable: false
    
                        delegate: RowLayout {
                                anchors.fill: parent
                                clip: true
    
                                IconButton {
                                    iconName: "content/create"
                                    onClicked: console.log(styleData.row)
                                }
    
                                IconButton {
                                    iconName: "action/delete"
                                    onClicked: {
                                        console.log(styleData.row)
                                        sqlSortedData.eliminaCliente(styleData.row)
                                    }
                                }
                        }
                    }
                    model: sqlSortedData
                }
    
     Q_INVOKABLE void eliminaCliente(int row) {
            QSqlTableModel *sm = (QSqlTableModel*)sourceModel();
            sm->removeRow(row);
    }
    

    Ok, so where is the problem? The code correctly removes the columns that are part of the model, but this isn't the case for the last column, the one where I use the delegate to show the edit and delete buttons.

    I cannot figure out why it works this way.



  • because they are not the part of the model?



  • I thought the same. This implies that TableView redraws everything each time the model changes, right?

    I think I don't have a clear view of how TableView instantiates the row objects. If a row is not present in the model during the redraw operation, why the corresponding custom column is instantiated?


Log in to reply