QML(08)——键盘事件key以及focus

本文介绍了在QML中如何使用focus和Keys.onPressed事件结合,以及如何设置Loader组件的焦点以便更好地控制子界面。重点展示了如何在接收到特定键(如Key_A)时阻止事件向上传播并执行相应操作。
摘要由CSDN通过智能技术生成

一、focus and key are generally used in combination

Rectangle {
    width: 100; height: 100
    focus: true
    Keys.onPressed: {
        if (event.key == Qt.Key_A) {
            console.log('Key A was pressed');
            //  prevent key events from propagating to parent class
            event.accepted = true;    
        }
    }
}

QML中的Keys事件_Luck-Dream的博客-CSDN博客_qml 焦点
https://blog.csdn.net/DreamSonGO/article/details/70661164
  


二、if a loader is used, the focus of loader must be set to true

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

    Loader {
        id: context
        anchors.fill: parent
        focus: true
        source: "DemoKey.qml"
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值