本文介绍了QML2 ApplicationWindow按键处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以处理QtQuick.Controls组件的ApplicationWindow中的按键事件? Qt5.3的文档没有提供任何方法来执行此操作.另外,它说Keys仅存在于Item -objects中.当我尝试处理按键事件时,它说"无法将按键属性附加到:ApplicationWindow_QMLTYPE_16(0x31ab890)不是项":

Is there any way to handle key press events in ApplicationWindow of QtQuick.Controls component? Documentation of Qt5.3 does not provide any way to do this. Also, it says that Keys is only exists in Item-objects . When I try to handle key press event it says "Could not attach Keys property to: ApplicationWindow_QMLTYPE_16(0x31ab890) is not an Item":

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 {}
  }
}

这篇关于QML2 ApplicationWindow按键处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:00