Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a button click when the enter key is pressed in QML

Tags:

qt

qml

I have some buttons and associated functions in my qml code, I want one button to be triggered when the enter key is pressed. All the buttons are handled by onClick event, ie, when the button is clicked my mouse,it executes the associated functions. I want to execute the click when the enter key is pressed

like image 395
Blaze Avatar asked Feb 05 '19 07:02

Blaze


1 Answers

You can react to different signals:

Button {
    id: _button
    text: "Button"

    function activate() { console.debug("Button activated"); }

    onClicked: _button.activate()
    Keys.onReturnPressed: _button.activate() // Enter key
    Keys.onEnterPressed: _button.activate() // Numpad enter key
}

Your button needs the focus to be notified about the key-press. If you want one specific button to have the focus by default, just add in the button

focus: true
like image 134
ymoreau Avatar answered Nov 04 '22 19:11

ymoreau