從Qt官方文檔學習qml(二)基於qml的鍵盤點擊和鼠標單擊事件

平臺:qtcreator 5.12.6  win10

取材於:https://doc.qt.io/qt-5.12/qtquick-layouts-example.html 官網列子

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        width: 200
        height: 100

        Text {
            id:text
            font.pointSize: 15
            anchors.centerIn: parent
            text: qsTr("text")
        }

// 鍵盤響應
        focus: true
        Keys.onPressed: {
            if (event.key === Qt.Key_Return) {   //約等於啓用鍵盤追蹤
                text.color = "red";
                event.accepted = true; //爲了讓父級不響應同一個事件
            }
        }
    }
//鼠標響應(觸摸)
    TapHandler{

        onTapped: text.color="blue"
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章