Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically press QML Button?

Tags:

qt

qml

qtquick2

I am building a dialog in QML. I have some TextField, but I'd like that if the user presses enter (accepted signal is emitted), the id: okButton is pressed, actually activating it visually for a moment.

I see that the pressed property is read only.

Thanks!

like image 576
Zsolt Szatmari Avatar asked Sep 06 '15 16:09

Zsolt Szatmari


2 Answers

You can simply call clicked() signal for simulating button press:

Keys.onReturnPressed: {
    clicked()
    event.accepted = true
}
like image 187
Vasiliy Avatar answered Sep 30 '22 18:09

Vasiliy


You can make it checkable for a short duration while you emulate the click with the checked property:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    Timer {
        id: timer
        running: true
        repeat: true
        interval: 100
        onTriggered: {
            button.checked = false;
            button.checkable = false;
        }
    }

    Row {
        TextField {
            anchors.verticalCenter: parent.verticalCenter

            onAccepted: {
                button.checkable = true;
                button.checked = true;
                timer.start();
            }
        }

        Button {
            id: button
            text: "Submit"
            anchors.verticalCenter: parent.verticalCenter
        }
    }
}

From the documentation for the accepted() signal of TextField:

This signal is emitted when the Return or Enter key is pressed.

like image 21
Mitch Avatar answered Sep 30 '22 18:09

Mitch