首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QtQuick TableView删除行不起作用

QtQuick TableView删除行不起作用
EN

Stack Overflow用户
提问于 2016-05-25 18:49:58
回答 1查看 298关注 0票数 0

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

remove row操作无法正常工作。我在一个从QSortFilterProxyModel派生的类中实现了一个方法来调用QSortFilterProxyModel的removeRows方法。

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

代码语言:javascript
复制
       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
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2016-06-14 21:33:14

看看here吧。这里有一个变通的建议。看起来QSortFilterProxyModel需要很长一段时间的爱。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37434881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档