[Interest] Qml SpinBox Control 2 undo/redo

Jérôme Godbout godboutj at amotus.ca
Wed Mar 18 16:51:21 CET 2020


Yeah I end up with something similar:

import QtQuick 2.14
import QtQuick.Controls 2.14

SpinBox
{
    id: component

    signal undoRedoValues(int prev_value, int new_value)

    // prepare values for undo/redo
    property var previousValues: [0]
    onValueChanged:
    {
        var prev = previousValues;
        prev.push(value);
        previousValues = prev.slice(-2);
    }

    onValueModified:
    {
        component.undoRedoValues(previousValues[0], value);
    }
}


From: Furkan Uzumcu <furkanuzumcu at gmail.com>
Sent: March 18, 2020 11:20 AM
To: interest at qt-project.org; Jérôme Godbout <godboutj at amotus.ca>
Subject: Re: [Interest] Qml SpinBox Control 2 undo/redo

Not very sophisticated but something like the following might work for you:


    1         SpinBox {

    2             id: spin

    3

    4             property var history: []

    5             property bool _internalUpdate: false

    6

    7             from: 0

    8             to: 100

    9             onValueChanged: {

   10                 if (!spin._internalUpdate) {

   11                     spin.history.push(value)

   12                 }

   13             }

   14             Component.onCompleted: {

   15                 spin.history.push(value)

   16             }

   17         }

   18

   19         Button {

   20             text: <="" span="">

   21             onClicked: {

   22                 if (spin.history.length > 0) {

   23                     spin._internalUpdate = true

   24                     spin.value = spin.history[

   25                         Math.max(0, spin.history.length - 2)

   26                     ]

   27                     spin._internalUpdate = false

   28                     if (spin.history.length > 1) {

   29                         spin.history.splice(spin.history.length - 1, 1)

   30                     }

   31                 }

   32             }

Regards,
Furkan Üzümcü
On Mar 17, 2020, 16:26 -0400, Jérôme Godbout <godboutj at amotus.ca<mailto:godboutj at amotus.ca>>, wrote:

Hi,
I’m trying to make an undo redo, I manage to have my whole thing working for user mouse drag n drop, add remove part into my model. One thing that bug me with the control 2 is the spinbox, the value is bind to the mode and the model get changed too, not sure this is the optimal way (I so wish we got 2 way binding like C# one day, this is so deeply needed and not have everybody reimplement there own way).

SpinBox
{
    id: sequenceIndex_

    value: opsModel.index

    onValueChanged:
    {
        // This is called before value modified
    }

    onValueModified:
    {
        // The value is already modified, how can I get previous value to make my undo/redo ? seem too late and the value changed as already been called
    }

    Binding
    {
        target: ops_model
        property: "index"
        value: sequenceIndex_.value
    }
}

How does one manage to do it with this control?! I guess I will have the same challenge with the ComboBox and all the selector controls (button are fairly easy since I can capture the previous value before doing the action).
_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200318/6fd723e4/attachment.html>


More information about the Interest mailing list