0

I'm trying to create a custom CustomSpinBox in QML and I want to increase or decrease the value from stepSize based on which button is going to be pressed. For example, if I am pressing the UP button I want to increase the value by X and if I am pressing the DOWN button I want to decrease the value by Y.

I know about up.pressed and down.pressed but those are just properties, how can I get their value inside on stepSize function?

CustomSpinBox.qml

CustomSpinBox{
....
}

Usage:

CustomSpinBox{
    id: maximumSpeed


    maximumValue: 100
    minimumValue: 0.01
    suffix: " " + qsTr("m/s")
    decimals: 3
    value: connection && connection.maxSpeed.value > 0 ? connection.maxSpeed.value : 1.5
    enabled: idOverrideDefault.checked

    stepSize: {
        // if UP is pressed return UP_VALUE
        // if DOWN is pressed return DOWN_VALUE
    }

    onValueChanged: {
        if (connection) {
            editor.handleSpeedRestriction(idOverrideDefault.checked, value)
        }
    }
}

UPDATE:

Well, I have a number, for example, 1. If you will press the UP button is going to become 2, 3, 4, and so on. But If you will press the DOWN button it will become 0.9, 0.8, 0.7 .... 0.1 if you will press again the DOWN button it will become 0.09, 0.08, ... 0.01. The problem that I have is that I don't know how can I achieve something like this without knowing whether is going to go UP or DOWN.

I mean for example if my value is 1 I want that if I will press UP my stepSize to be 1, but if I will press DOWN to be 0.1 The same if my value will be 0.1, if I will press UP then my stepSize should be 0.1 but if I will press DOWN should be 0.01.

Because of this problem I need to know which button is pressed at each time.

Also if I am trying to write maximumSpeed.up.pressed is going to give me this error message: Cannot read property 'pressed' of undefined Also if I am going to define my property insider of CustomSpinBox for example something like this property bool isUp: root.up.pressed then this is never going to be updated so I will always have a false over there.

Mircea
  • 1,671
  • 7
  • 25
  • 41
  • I don't think changing `stepSize` based on which button is pressed will work. I expect that by the time you update its value, the increase/decrease functions will already have been called. Or at least, you won't be able to rely on the order of events. I think you will need to create your own buttons and manually tell them what to do when they are pressed. – JarMan Mar 18 '21 at 14:35
  • is there is no way in which I can get which button was pressed inside my stepSize function? – Mircea Mar 18 '21 at 14:46
  • something like that: `property var someValue: down.pressed ? -1 : (up.pressed ? 1 : 0)`? – folibis Mar 18 '21 at 15:03
  • that's a good idea except that... How do I update my property? I mean if I will say something like this it will be always `false` because it will be once initialized and never updated. How can I update this in this case? – Mircea Mar 18 '21 at 15:09
  • And I couldn't find anything like `up.onPressed` or something like this – Mircea Mar 18 '21 at 15:10
  • @folibis showed you how to do it: `stepSize: down.pressed ? -1 : (up.pressed ? 1 : 0)`. I just don't think it will work because of the reasons I stated above. But maybe I'm wrong. – JarMan Mar 18 '21 at 15:20
  • I did update my question explaining why is either not working or I have no idea how to make it work. – Mircea Mar 18 '21 at 15:23
  • did you try my solution? – folibis Mar 18 '21 at 18:43
  • yes, I did but is not working as I explain in my **UPDATE**. Basically, if I am trying to use something like this `root.up.pressed` where root is my CustomSpinBox ID I will get an error message such `Cannot read property 'pressed' of undefined` (same if I am not using root and I am using directly `up`). I'm not sure what I am doing wrong but is not working – Mircea Mar 18 '21 at 19:57

1 Answers1

2

You should use property group syntax as :

SpinBox
{
    property bool isUpPressed: up.pressed
    property bool isDownPresed: down.pressed

    up.onPressedChanged: {
        isUpPressed = up.pressed
    }
    down.onPressedChanged: {
        isDownPresed = down.pressed
    }
}

for Quick.Controls 1. where up and down are not available, this trick can be used :

   SpinBox
    {
        property int oldValue: -1
      
        onValueChanged: {
            if (value > oldValue)
            {
                console.log("up")
            }
            else
            {
                console.log("down")
            }
            oldValue = value
        }
    }
Muhammet Ali Asan
  • 1,486
  • 22
  • 39
  • This is not working, I'm getting the following error message if I am trying to add this code into my `SpinBox` item. `Cannot assign to non-existent property "down" down.onPressedChanged: {` – Mircea Mar 18 '21 at 23:46
  • And I am also getting the same error also for `up` not only `down` – Mircea Mar 18 '21 at 23:49
  • Which qt version are you using? And how do you import your controls in qml? Can you try my answer with "import QtQuick.Controls 2.0" – Muhammet Ali Asan Mar 19 '21 at 01:11
  • Well regarding the QT version is 5.14.2 and regarding the QtQuick I am using 1.4. If would be to use 2.0 I have a problem that is not recognizing the `style` property and is getting an error `Cannot assign to non-existent property "style" style: SpinBoxStyle {` – Mircea Mar 19 '21 at 07:48
  • And I just found that style is not available in QtQuick.Controls 2.0.What should I do in this case? – Mircea Mar 19 '21 at 08:01
  • Also, I have multiple problems if I am going to use QtQuick.Controls 2.0 as I've just checked. Do you know any possibility of how can I achieve this without using 2.0? – Mircea Mar 19 '21 at 08:28
  • This is working, thanks you so much for your help :) – Mircea Mar 19 '21 at 16:48