第14章:QML之KeyEvent

1,按键事件
2,源码

import QtQuick 2.6
Rectangle {
    property alias mouseArea: mouseArea

    MouseArea {
        id: mouseArea
        anchors.fill: parent
    }

    Row{
        x: 50; y: 50
        spacing: 30

        Rectangle{
            id: music
            width: 100; height: 100
            radius: 6
            color: focus ? "red" : "lightgray"          //被选中时变红色或者变灰色
            scale: focus ? 1.0 : 0.8                    //被选中是图标变大
            focus: true
            KeyNavigation.tab: play
            Keys.onUpPressed: music.y    -= 10             //上移
            Keys.onDownPressed: music.y  += 10             //下移
            Keys.onLeftPressed: music.x  -= 10             //左移
            Keys.onRightPressed: music.x += 10             //右移

            Text{
                anchors.centerIn: parent
                color: parent.focus ? "black" : "gray"     //被选中时显示黑字,否则变灰
                font.pointSize: 20                         //字体大小
                text: "音乐"                                //文字内容为音乐
            }
        }

        Rectangle{
            id: play
            width: 100; height: 100
            radius: 6
            color: focus ? "green" : "lightgray"
            scale: focus ? 1 : 0.8
            KeyNavigation.tab: movie                       //将焦点移动到影视图标
            Keys.onUpPressed:   play.y  -= 10
            Keys.onDownPressed: play.y  += 10
            Keys.onLeftPressed: play.x  -= 10
            Keys.onRightPressed: play.x += 10

            Text{
                anchors.centerIn: parent
                color: parent.focus ? "black" : "gray"
                font.pointSize: 20
                text : "游戏"
            }
        }

        Rectangle{
            id: movie
            width: 100; height: 100
            radius: 6
            color: focus ? "blue" : "lightgray"
            scale: focus ? 1.0 : 0.8
            KeyNavigation.tab: music                        //将焦点移动到 music上
            Keys.onPressed: play.y      -= 10
            Keys.onDownPressed: play.y  += 10
            Keys.onLeftPressed: play.x  -= 10
            Keys.onRightPressed: play.x += 10

            Text{
                anchors.centerIn: parent
                color: parent.focus ? "black" : "gray"
                font.pointSize: 20
                text: "影视"
            }
        }
    }
}

3,效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值