Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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: 045

    What I want is:

    Display: 0
    *user clicks +/-
    Display: -
    *user enters the number (45)
    Display: -45

    I 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
            }
        }
    

  • Moderators

    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:-

    0_1560410563728_c5537bfd-b2c7-4bbb-9e99-022b6c69c9c2-image.png

    Case 1:- Not entering the value clicking on the button

    0_1560410577890_699d50d0-032d-41bf-bc93-4d41f0a366e3-image.png

    Case 2:- Just Entering the value

    0_1560410615184_64242a91-d96e-4ddf-9069-2b84e75fa013-image.png

    Case 3:- Entering the value and clicking on the button

    0_1560410637501_e8bd5d66-bec6-499d-97b1-c0b9f5f4be9f-image.png



  • This post is deleted!


  • @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: -45

    which 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: 45

    So 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: -45

    so 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
            }
        }

Log in to reply