Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. My +/- PushButton

My +/- PushButton

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 767 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Circuits
    wrote on last edited by Circuits
    #1

    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
            }
        }
    
    J.HilkJ 1 Reply Last reply
    0
    • C Circuits

      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
              }
          }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      C 1 Reply Last reply
      2
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by
        #3

        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

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        2
        • C Offline
          C Offline
          Circuits
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            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)
                    }
                }
            
            C Offline
            C Offline
            Circuits
            wrote on last edited by Circuits
            #5

            @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.

            1 Reply Last reply
            0
            • Shrinidhi UpadhyayaS Offline
              Shrinidhi UpadhyayaS Offline
              Shrinidhi Upadhyaya
              wrote on last edited by
              #6

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

              Shrinidhi Upadhyaya.
              Upvote the answer(s) that helped you to solve the issue.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved