QML2 ApplicationWindow键处理

时间:2014-11-11 14:05:47

标签: qt event-handling window key qml

有没有办法处理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;
    }
}

2 个答案:

答案 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()
        }
    }
}