有没有办法处理QtQuick.Controls
组件的ApplicationWindow中的按键事件? Qt5.3的文档没有提供任何方法来执行此操作。此外,它表示Keys
仅存在于Item
- 对象中。当我尝试处理按键事件时,它说“无法将Keys属性附加到:ApplicationWindow_QMLTYPE_16(0x31ab890)不是项目”:
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.1
import QtQuick.Window 2.1
ApplicationWindow {
id: mainWindow
visible: true
width: 720
height: 405
flags: Qt.FramelessWindowHint
title: qsTr("test")
x: (Screen.width - width) / 2
y: (Screen.height - height) / 2
TextField {
id: textField
x: 0
y: 0
width: 277
height: 27
placeholderText: qsTr("test...")
}
Keys.onEscapePressed: {
mainWindow.close()
event.accepted = true;
}
}
答案 0 :(得分:8)
ApplicationWindow {
id: mainWindow
Item {
focus: true
Keys.onEscapePressed: {
mainWindow.close()
event.accepted = true;
}
TextField {}
}
}
答案 1 :(得分:2)
也许这会有所帮助。
使用Shortcut
不需要设置focus
。
ApplicationWindow {
id: mainWindow
Shortcut {
sequence: "Esc"
onActivated: mainWindow.close()
}
}
}