oxid2178
oxid2178

Reputation: 45

QML disable button after other button_click event and enable again after timer timeout

I would disable button after other button_click event and enable again after timer timeout. I tried with Timer and States with PropertyChanged, but it doesn't work. How I can send a qml signal to reload/refresh the page?

When i click on id_button_add I would disable Id_button_edit and start a timer for 3 seconds. When the timer timeouts I enable already the id_button_edit.

Here is my code.

import QtQuick 2.0
import QtQuick.Layouts 1.3

import "../items"
import ch.example 1.0

Item {
id: id_root
z: 2

property bool prepare_delete: false
property string button_edit_enable_state: "enabled"

anchors.left: parent ? parent.left : undefined
anchors.right: parent ? parent.right : undefined

Connections {
    target: id_root.ListView.view
    onCurrentIndexChanged: {
        prepare_delete = false
    }
}

function update_remove_enable() {
    id_button_remove.button_enabled = ui_resident_model.sourceModel.rowCount() > 1
}

Component.onCompleted: {
    ui_resident_model.sourceModel.onRowsInserted.connect(update_remove_enable)
    ui_resident_model.sourceModel.onRowsRemoved.connect(update_remove_enable)
    update_remove_enable()
}

Rectangle {
    anchors.fill: parent

    color: "transparent"
    border {
        color: touchpanel_style.foreground_color
        width: touchpanel_style.line_width
    }
}

RowLayout {
    anchors.left: parent.left
    anchors.margins: touchpanel_style.margin
    anchors.verticalCenter: parent.verticalCenter

    .
    .
    .
    .
}

RowLayout {
    id: id_content
    anchors.right: parent.right
    anchors.margins: touchpanel_style.margin
    anchors.verticalCenter: parent.verticalCenter

    state: button_edit_enable_state

    states: [
        State {
            name: "enabled"
            PropertyChanges { target: id_button_edit; enabled: true }
        },
        State {
            name: "disabled"
            PropertyChanges { target: id_button_edit; enabled: false }
        }
    ]

    Timer {
        id: serviceListItemTimer
        interval: 3000
        running: false
        repeat: false
        onTriggered: {
            console.log("onTriggered")
            button_edit_enable_state = "enabled"
        }
    }

    IconButton {
        id: id_button_edit
        objectName: "button_edit"
        Layout.fillHeight: true
        width: height

        icon_factor: 1.35
        icon_source: "../icons/edit.png"

        onButtonClick: {
            prepare_delete = false
            loader.source = "../service_menu/ResidentEdit.qml";
            loader.item.onCancel.connect(cancel_edit)
            loader.item.onTimeout.connect(timeout)
            loader.item.model = id_root.ListView.view.currentItem.resident_model
        }

        function cancel_edit() {
            loader.source = ""
        }
    }

    IconButton {
        id: id_button_add
        objectName: "button_add"
        Layout.fillHeight: true
        width: height

        icon_factor: 1.35
        icon_source: "../icons/resident_add.svg"

        onButtonClick: {
            console.log("btn_add")
            button_edit_enable_state = "disabled"
            serviceListItemTimer.running = true
            var index = id_root.ListView.view.currentIndex;
            id_root.ListView.view.model.createItem(index);
            set_index(index);
        }
    }
}

}

Upvotes: 1

Views: 12302

Answers (3)

oxid2178
oxid2178

Reputation: 45

I found a solution --> bind qml property with cpp model and emit also in cpp module valueChanged() signal. see http://imaginativethinking.ca/bi-directional-data-binding-qt-quick/

Upvotes: 0

Kisbandi
Kisbandi

Reputation: 313

If you use enabled property of the item, you can disable the button.

For example:

IconButton {
        id: id_button_edit
        ...
        enabled: true
        ...
        onButtonClick: {
           id_button_edit.enabled = false;
           ...
        }

Then the timer expire, you return to enable the button with id_button_edit.enabled = true.

Maybe an another solution can be the signal.

Here is more info: https://doc.qt.io/qt-5/qtqml-syntax-signals.html

Upvotes: 4

GrecKo
GrecKo

Reputation: 7150

You can do it in a simple an declarative way by binding the enabled property of your edit button to the running property of the timer, no need to deal with states and such:

IconButton {
    id: id_button_add
    // ...
    onButtonClick: {
        serviceListItemTimer.start();
        var index = id_root.ListView.view.currentIndex;
        id_root.ListView.view.model.createItem(index);
        set_index(index);
    }
}

IconButton {
    id: id_button_edit
    enabled: !serviceListItemTimer.running
    // ...
}

Upvotes: 0

Related Questions