Guys, there is a question about qt6 tableview

Viewed 18

i'm trying to implements a table editor just like QTableView.Now,i rewrite c++ QAbstractItemModel apply to qml model attribute."tableview.selectionModel" choose "ItemDelegateModel".how can i click the item with a Rectangle editor change the model data.at the same time the row i select is highlight.here is code:

import QtQuick
import QtQuick.Controls 2.5
import QtQuick.Window 2.3
import QtQml 2.3
import TableModel 1.0
Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")


    Rectangle{
        width:parent.width
        height: parent.height
        anchors.fill: parent
        color:"black"


        TableView{

            id:tableView
            anchors.fill: parent
            focus:true
            columnSpacing: 1
            rowSpacing: 1
            clip: true
            //设置行列宽度
            columnWidthProvider: function (column) {
                return 100
            }
            rowHeightProvider: function (row){
                return 15
            }
            //关闭拖动效果哦
       //     boundsBehavior: Flickable.StopAtBounds

            //标题栏填充
            topMargin: columnsHeader.implicitHeight
            //水平标题栏填充
            leftMargin: 0
            //item display委托
            delegate:Component{
                id:com
                Rectangle{
                    id:itemRec
                    color: "white"

                Text {
                    id:editText
                    text: display
                    anchors.fill: parent
                    anchors.margins: 10
                    color: 'black'
                    font.pixelSize: 10
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter

                }
            }
            }

            MouseArea{
                id:area
                anchors.fill: parent
                property point index: tableView.cellAtPos(Qt.point(area.mouseX,area.mouseY+tableView.topMargin))
                onClicked: {
                    console.log(table_Model.data(index))
                    tableView.selectionModel.select(table_Model.index(index.y,index.x),ItemSelectionModel.SelectCurrent)
                    tableView.selectionModel.setCurrentIndex(table_Model.index(index.y,index.x),ItemSelectionModel.SelectCurrent)

                }

            }

            selectionModel: ItemSelectionModel{
                id:selection
                model:table_Model
                onSelectionChanged: {
                    console.log(selection.currentIndex)

                }
            }
            model:TableModel{
                id:table_Model
            }





          //  加载标题头
            Row {
                id: columnsHeader
                x:tableView.contentX
                y: tableView.contentY
                z: 2
                Repeater {
                    model: tableView.columns > 0 ? tableView.columns : 1
                    Label {
                        width: tableView.columnWidthProvider(modelData)+tableView.columnSpacing
                        height: tableView.rowHeightProvider(modelData)+tableView.rowSpacing
                        text: table_Model.headerData(modelData, Qt.Horizontal)
                        color: '#aaaaaa'
                        font.pixelSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        background: Rectangle { color: "grey" }
                    }

                }


            }


        }
    }


}
1 Answers

TableView's selectionModel is broken as of Qt 6.3

Related