gpt4 book ai didi

qt - 如何在鼠标单击 QtQuick2 TableView 时获取列号

转载 作者:行者123 更新时间:2023-12-03 23:51:46 25 4
gpt4 key购买 nike

我正在尝试处理表格单元格上的鼠标点击。只有当我单击某个单元格时,我才需要响应右键。但我找不到如何捕捉列号。使用来自 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
}
}
}
}
}
}

最佳答案

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

// ...
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 .

关于qt - 如何在鼠标单击 QtQuick2 TableView 时获取列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com