paolino
paolino

Reputation: 31

QtQuick TableView delete row doesn't work

I am using QtQuick TableView to show data from a database through QSqlTableModel and QSortFilterProxyModel.

The remove row operation doesn't work as it should. I have implemented a method in a class derived from QSortFilterProxyModel to call removeRows methods of QSortFilterProxyModel.

Everything works correctly as long as I have a filter setted in QSortFilterProxyModel ( i set it through a text box ). But when the filter is empty, the TableView rowCount property doesn't decrement and, after each delete, the currentRow property is set to rowCount-2. Why? To me it looks like a bug. Why it works when the filter is not empty?

       Q_INVOKABLE void eliminaCliente(int row) {
            removeRows(row,1);
        }

import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.2
import Material 0.2
import Material.ListItems 0.1

ApplicationWindow {
    id: root
    visible: true
    width: 1024
    height: 640
    title: qsTr("assiBase")

    Page {
        id: pLayout
        anchors.fill: parent

        ColumnLayout {
            anchors.fill: parent

            Toolbar {
                id: aBar
                Layout.fillWidth: true
                page: pLayout
                backgroundColor: "#eeeeee"

                RowLayout {
                    anchors.fill: parent

                    ActionButton {
                        id: addButton
                        Layout.leftMargin: 10
                        iconName: "content/add_circle"
                        backgroundColor: "#4CAF50"
                        onClicked: modalDialog.show()
                        isMiniSize: true
                    }

                    ActionButton {
                        id: editButton
                        iconName: "content/create"
                        isMiniSize: true
                    }

                    ActionButton {
                        id: deleteButton
                        iconName: "action/delete"
                        isMiniSize: true
                        backgroundColor: "#FF0000"
                        onClicked: {
                            if (dataView.currentRow != -1) {
                                var r = dataView.currentRow
                                console.log(dataView.currentRow)
                                sqlSortedData.eliminaCliente(dataView.currentRow)
                                console.log(dataView.rowCount)
                                //dataView.currentRow = r

                            }
                        }
                    }

                    RowLayout {
                        Layout.alignment: Qt.AlignRight

                        Icon {
                            name: "action/search"
                            Layout.alignment: Qt.AlignBottom
                        }

                        TextField {
                            id: searchBox
                            Layout.rightMargin: 20
                            Layout.minimumWidth: 400
                            Layout.preferredWidth: 500
                            placeholderText: qsTr("cerca...")
                            onTextChanged: sqlSortedData.setFilterWildcard(searchBox.text)
                            font.capitalization: Font.MixedCase
                        }
                    }
                }
            }

            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: "ID"
                    visible: false
                }

                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)
                                    console.log(dataView.rowCount)
                                }
                            }
                    }
                }

                model: sqlSortedData
            }
        }
    }

Upvotes: 0

Views: 390

Answers (1)

urkon
urkon

Reputation: 263

Take a look at here. There is an workaround suggestion. It seems like QSortFilterProxyModel needs some love for a long time.

Upvotes: 0

Related Questions