QML新功能——TapHandler信号处理器

TapHandler是用于触摸屏上的点击或鼠标单击的信号处理器。使用时需要导入import QtQuick 2.12

举例来说,对于以前的Rectangle,如果鼠标点击时执行某代码块,一般会在该Rectangle上添加一个子对象MouseArea。在新的QML版本中可使用TapHandler。

TapHandler有效敲击手势的检测取决于gesturePolicy。 默认值为DragThreshold,它要求按下和释放键在空间和时间上都紧密靠近。

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle {
        width: 600
        height: 400
        anchors.centerIn: parent
        color: "lightgray"
        TapHandler {
            //点击屏幕时,修改了pressed属性,触发onPressedChanged
            onPressedChanged: {
                console.log("press ? : ", pressed)
            }
            //长按时触发onLongPressed
            onLongPressed: {
                console.log("long pressed")
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章