如何在单击QtQuick2 TableView时获得列号

时间:2019-06-07 16:58:40

标签: qt qml tableview qt5 qtquick2

我正在尝试处理对表格单元格的鼠标单击。只有在某个单元格中单击时,我才需要响应右按钮。但我找不到如何捕获列号。使用QtQuick 2.12中的TableView。或者,也许您可​​以不用列号?由于各种原因,QtQuickControls I的Tableview无法使用。

TableView {
    id: table
    boundsBehavior: Flickable.StopAtBounds
    anchors.fill: parent
    columnSpacing: 0
    rowSpacing: 0
    anchors.rightMargin: 2
    anchors.leftMargin: 5
    anchors.bottomMargin: 5
    anchors.topMargin: 70
    clip: true

    model: TableModel {
        id: model
        Component.onCompleted: {
            model.init()
        }
    }

    delegate: Rectangle {
        id: tableDelegate

        implicitWidth: textDelegate.implicitWidth + textDelegate.padding * 2
        implicitHeight: 30
        border.width: 0

        TextField {
            id: textDelegate
            text: tabledata
            anchors.fill: parent
            anchors.verticalCenter: parent.verticalCenter
            clip: true
            horizontalAlignment: TextField.AlignHCenter
            verticalAlignment: TextField.AlignVCenter

            enabled: true

            background: Rectangle {
                border.color: "#e61d6887"
                color: "#e6ffffff"
                border.width: 1
            }
            selectByMouse: true

            MouseArea {
                id: mad
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton
                onClicked: {
                    switch(mouse.button){
                    case Qt.RightButton:
                        console.log("right button")
                        dialog.open()

                        break
                    case Qt.LeftButton:
                        console.log("left button")
                        break
                    default:
                        break
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您必须使用model.row和model.column(或行和列)分别获取委托中的行或列。

// ...
MouseArea {
    id: mad
    anchors.fill: parent
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    onClicked: {
        console.log(model.row, model.column)
        // or
        // console.log(row, column)
        // ...
     }
// ...

我在文档QTBUG-76529中报告了该错误。

相关问题