My +/- PushButton
-
Here is how my +/- button on this number pad is currently written:
PushButton { width: 75 height: width text: "+/-" enabled: settingValue.minValue < 0 onClicked: { outValue=outValue * -1; } }
This works so long as the user enters a number first and then hits the +/- button. However, with the current implementation when a user hits the button before entering a number the result is that outValue gets set to 0, for instance:
Display: 0
*user clicks +/-
Display: 0
*user enters the number (45)
Display: 045What I want is:
Display: 0
*user clicks +/-
Display: -
*user enters the number (45)
Display: -45I was hoping someone might have a clever way of achieving this functionality.
Edit: It might be relevant to know that my num pad translates outValue into a number when the pad is closed, ie:
property var selectionHandler: function(index){ switch(index){ case 0: control.destroy() break; case 1: settingValue.value=Number(outValue) control.destroy(); break; case 2: if(outValue.length!=0) control.destroy() else break; } }
so outValue is declared as a string. So settingValue could be accessed directly and I can also alter the display text by chaning entryDisplay.text. Here is how entryDisplay.text is setup:
onOutValueChanged: { if(outValue ===""){ entryDisplay.text = settingValue.value } else { entryDisplay.text = outValue } }
-
hi @Circuits
I would suggest the following:
PushButton { id:plusMinus width: 75 height: width text: "+/-" enabled: settingValue.minValue < 0 property bool isMinus: false onIsMinusChanged: entryDisplay.text = entryDisplay.text * (plusMinus.isMinus ? -1 : 1) onClicked: { isMinus = !isMinus } } .... onOutValueChanged: { if(outValue ===""){ entryDisplay.text = settingValue.value } else { entryDisplay.text = outValue * (plusMinus.isMinus ? -1 : 1) } }
-
Hi @Circuits , with what i understand here is the sample code:-
property int signVal: 1 property int outValue: signVal * txtArea.text property string dummyValue: (signVal === -1 && outValue === 0) ?'-' :'' Rectangle { id: dummyRect width: 300 height: 40 color: "transparent" border.color: "black" TextInput { id: txtArea width: 150 height: parent.height verticalAlignment: Text.AlignVCenter //####Dummy Rectangle so that you know where to enter the text Rectangle { anchors.fill: parent color: "transparent" border.color: "red" } } TextArea { id: txt width: 150 height: parent.height text: "Display Value:" + (dummyValue=== '' ? outValue : dummyValue) anchors.left: txtArea.right verticalAlignment: Text.AlignVCenter } } Button { id: dummyButton height: 40 width: 100 text: "+/-" anchors.left: dummyRect.right MouseArea { anchors.fill: parent onClicked: { signVal = -1 } } }
Default:-
Case 1:- Not entering the value clicking on the button
Case 2:- Just Entering the value
Case 3:- Entering the value and clicking on the button
-
@J.Hilk For some reason it's not displaying a - when I first click it (like it seems like it should):
Display: 0
*user clicks +/-
Display: 0
*user enters the number (45)
Display: -45which is pretty good, certainly an improvement but there is a problem or two:
Display: 0
*user clicks +/-
Display: 0
*user enters the number (45)
Display: -45
*user clicks +/-
Display: -45
*user clicks +/-
Display: 45So as you can see the user has to hit the +/- button twice in order to alter the sign. Also:
Display: 0
*user enters (45)
Display: 45
*user clicks +/-
Display: -45
*user clicks +/-
Display: -45
*user clicks +/-
Display: 45
*user clicks C
*user enters (45)
Display: -45so it's still a bit wonky. I will give Shrinidhi's logic a try.
-
Hi @Circuits , just 2 things to correct in my code:-
-
Remove the complete MouseArea code inside the button(my bad).
-
I guess i just missed the logic a bit. So inside the Button after removing MouseArea add this code:-
onClicked: { if(signVal === 1) { signVal = -1 } else { signVal = 1 } }
-