【问题标题】:QtQuick TableView delete row doesn't workQtQuick TableView 删除行不起作用
【发布时间】:2016-05-25 10:49:57
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在根项目中跟踪您的 currentRow,然后尝试 tableView.model.remove(root.currentRow)
  • 感谢您的回复。但是我注意到 QSortFilterProxyModel 是罪魁祸首。实际上,当过滤器为空时,不会更新 rowCount。所以问题出在 QSortFilterProxyModel 而不是 TableView。

标签: qt qml tableview


【解决方案1】:

看看here。有一个解决方法建议。 QSortFilterProxyModel 似乎需要一些爱很久了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2015-02-23
    • 2018-02-02
    • 1970-01-01
    • 2013-12-20
    • 2013-10-31
    • 2011-05-01
    相关资源
    最近更新 更多