有什么方法可以处理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;
}
}
ApplicationWindow {
id: mainWindow
Item {
focus: true
Keys.onEscapePressed: {
mainWindow.close()
event.accepted = true;
}
TextField {}
}
}
也许这会对一些人有所帮助。使用Shortcut
不需要设置focus
。
ApplicationWindow {
id: mainWindow
Shortcut {
sequence: "Esc"
onActivated: mainWindow.close()
}
}
}