一、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"
}
}