Как получить номер столбца при щелчке мыши для QtQuick2 TableView - PullRequest
2 голосов
/ 07 июня 2019

Я пытаюсь обрабатывать щелчки мышью по ячейкам таблицы. Мне нужно отвечать на правую кнопку, только если я нажал в определенной ячейке. но я не могу найти, как поймать номер столбца. Используется TableView из QtQuick 2.12. Или, может быть, вы можете обойтись без номера столбца? Tableview от QtQuickControls я по разным причинам не могу использовать.

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 Ответ

1 голос
/ 08 июня 2019

Вы должны использовать 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 .

...