QtQuick TableView删除行不起作用

时间:2016-05-25 10:49:58

标签: qt qml tableview

我正在使用QtQuick TableView通过QSqlTableModel和QSortFilterProxyModel显示数据库中的数据。

删除行操作不能正常工作。我在从QSortFilterProxyModel派生的类中实现了一个方法来调用QSortFilterProxyModel的removeRows方法。

只要我在QSortFilterProxyModel中设置过滤器(我通过文本框设置),一切都能正常工作。但是当过滤器为空时,TableView rowCount属性不会减少,并且在每次删除后,currentRow属性将设置为rowCount-2。为什么?对我来说,它看起来像一个bug。当过滤器不为空时,为什么它可以工作?

       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
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

看看here。有一个解决方法的建议。 似乎QSortFilterProxyModel需要很长时间的爱。