如何在QML TableView标题上获得Clicked或Pressed事件?

时间:2018-05-18 20:05:01

标签: qt qml tableview

我正在尝试在QML TableView标题上获取点击或按下事件。

我已经尝试过HeaderDelegate创建一个来自父级的宽度,高度和anchors.fill的MouseArea,并获取onClickedonPressed个事件,但不起作用。

示例:

MouseArea {
    width: parent.width
    height: parent.height
    anchors.fill: parent

    onClicked: {
        console.debug("CLICKED: "+styleData.pressed)
        console.debug("COLUMN:  "+ styleData.column)
    }

    onPressed: {
        console.debug("PRESSED: "+styleData.pressed)
        console.debug("COLUMN:  "+ styleData.column)
    }
}

不记录任何内容

我也尝试在HeaderDelegate上使用“Connections”,使用“target:styleData”和“onPressedChanged”事件,并且工作,但“onPressedChanged”同时获得4次更改。 例如:

Connections {
    target: styleData
    onPressedChanged: {
        console.debug("PRESSED: "+ styleData.pressed)
        console.debug("COLUMN:  "+ styleData.column)
    }
}

返回:

qml: PRESSED: true 
qml: COLUMN:  0 
qml: PRESSED: true 
qml: COLUMN:  0 
qml: PRESSED: false 
qml: COLUMN:  0 
qml: PRESSED: false 
qml: COLUMN:  0 

TableView中的整个代码:

TableView  {
    id: tViewEnt
    width: parent.width
    height: parent.height
    anchors.verticalCenter: parent.verticalCenter
    anchors.horizontalCenter: parent.horizontalCenter
    verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
    horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff


    TableViewColumn {
        id: column0
        title: "column0"
        width: 30

        delegate: CheckBox {
            style: CheckBoxStyle {
                indicator: Rectangle {
                    //INDICATOR PROPERTIES
                }
            }
        }
    }
    TableViewColumn {
        title: "column1"
        role: "column1"
    }
    TableViewColumn {
        title: "column2"
        role: "column2"
    }


    headerDelegate: Rectangle {
        height: tViewEnt.height / 15
        color: styleData.column===0 ? "#4D4D4D" : "#0077B3"

        Rectangle {
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 1
            anchors.topMargin: 1
            width: 1
            color: "#333"
        }
        Text {
            text: styleData.value
            color: "#CFF"
            width: parent.width
            height: parent.height
            font.pointSize: 18
            minimumPointSize: 3
            fontSizeMode: Text.Fit
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        Connections {
            target: styleData
            onPressedChanged: {
                console.debug("PRESSED: "+ styleData.pressed)
                console.debug("COLUMN:  "+ styleData.column)
            }
        }
    }
}

那么,我如何以正确的方式获得Header Delegate点击或按下事件?

1 个答案:

答案 0 :(得分:0)

尝试一下

MouseArea{
    anchors.fill: parent
    hoverEnabled: true

    onEntered: {
        console.debug("clicked")
    }
}

这不是正确的代码。但这对我有用。