【问题标题】:How to get column number at mouse click for QtQuick2 TableView如何在鼠标单击 QtQuick2 TableView 时获取列号
【发布时间】:2019-06-07 16:58:40
【问题描述】:

我正在尝试处理对表格单元格的鼠标点击。只有当我单击某个单元格时,我才需要响应右键。但我找不到如何捕捉列号。使用来自 QtQuick 2.12 的 TableView。或者也许你可以不用列号? QtQuickControls中的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
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: qt qml tableview qt5 qtquick2


    【解决方案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

    【讨论】:

    • 谢谢,这正是您所需要的。太神奇了,我之前找不到这个信息
    • @АлексейБанченко 你是对的,要获得这个信息我必须检查源代码,我已经报告了这个错误
    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多