Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML2 ApplicationWindow Keys handling

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;
    }
}
like image 795
Victor Polevoy Avatar asked Nov 11 '14 14:11

Victor Polevoy


2 Answers

ApplicationWindow {
id: mainWindow
  Item {
    focus: true
    Keys.onEscapePressed: {
      mainWindow.close()
      event.accepted = true;
    }
  TextField {}
  }
}
like image 117
Meefte Avatar answered Oct 25 '22 00:10

Meefte


Maybe this will help some. Using a Shortcut doesn't require focus to be set.

ApplicationWindow {
    id: mainWindow

    Shortcut {
        sequence: "Esc"
        onActivated: mainWindow.close()
        }
    }
}
like image 28
bardao Avatar answered Oct 25 '22 02:10

bardao