首页 > 其他 > 详细

Rectangle 响应按键

时间:2015-08-27 22:28:33      阅读:272      评论:0      收藏:0      [点我收藏+]
import QtQuick 2.4
import QtQuick.Window 2.2


Window {
    visible: true
    MainForm {
        anchors.fill: parent

    }

    Rectangle{
        width: 300
        height: 200
        color: "#c1c1c1"
        focus:true
        Keys.enabled: true
        Keys.onEscapePressed: Qt.quit()
        Keys.onBackPressed: Qt.quit()

        Keys.onPressed: {
            switch(event.key){
            case Qt.Key_0:
            case Qt.Key_1:
            case Qt.Key_2:
            case Qt.Key_3:
            case Qt.Key_4:
            case Qt.Key_5:
            case Qt.Key_6:
            case Qt.Key_7:
            case Qt.Key_8:
            case Qt.Key_9:
                event.accepted=true
                keyView.text=event.key-Qt.Key_0
                break;
            }
        }
    }

    Text{
        id:keyView
        font{
            bold:true
            pixelSize: 24
        }
        text:qsTr("key")
        anchors.centerIn: parent
    }
}
Qt.Key_0为48,
Qt.Key_1为49依次类推,所有要减去Qt.Key_0才可以正常显示0,1,2...

Rectangle 响应按键

原文:http://www.cnblogs.com/yshyee/p/4764664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!