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. mouse.button with switch-case

mouse.button with switch-case

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 4 Posters 468 Views
  • 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.
  • hslee_6560H Offline
    hslee_6560H Offline
    hslee_6560
    wrote on last edited by hslee_6560
    #1

    Hi, I tried to use switch-case with mouse.button but, it seems that doesn't work.

    I want to know the reason.

    Why It doesn't work?

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.AllButtons
        // ...
        onPressed: {
            property string buttonID = 'Nothing'
    
            switch (mouse.button) {
            case Qt.LeftButton: buttonID = 'LeftButton'; break;
            case Qt.RIghtButton: buttonID = 'RIghtButton'; break;
            }
            test.text = 'pressed ' + buttonID
        }
    }
    

    I found it works whether I use if-else not switch-case.

    I can not understand what is the difference between two of them.

    Please, Let me know.

    Thank you! :)

    ps. I missed something like 'break' or 'acceptedButtons' ... but it were there when I found the problem, and I edited them. sorry.

    I am really sorry, It turned out my fault.
    I wrote wrong spell " switch (mouse.botton) { ..."
    now it works.
    Thanks all your comments.

    Pradeep P NP ODБOïO 2 Replies Last reply
    0
    • hslee_6560H hslee_6560

      Hi, I tried to use switch-case with mouse.button but, it seems that doesn't work.

      I want to know the reason.

      Why It doesn't work?

      MouseArea {
          anchors.fill: parent
          acceptedButtons: Qt.AllButtons
          // ...
          onPressed: {
              property string buttonID = 'Nothing'
      
              switch (mouse.button) {
              case Qt.LeftButton: buttonID = 'LeftButton'; break;
              case Qt.RIghtButton: buttonID = 'RIghtButton'; break;
              }
              test.text = 'pressed ' + buttonID
          }
      }
      

      I found it works whether I use if-else not switch-case.

      I can not understand what is the difference between two of them.

      Please, Let me know.

      Thank you! :)

      ps. I missed something like 'break' or 'acceptedButtons' ... but it were there when I found the problem, and I edited them. sorry.

      I am really sorry, It turned out my fault.
      I wrote wrong spell " switch (mouse.botton) { ..."
      now it works.
      Thanks all your comments.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #4

      hi,
      @hslee_6560 said in mouse.button with switch-case:

      onPressed: {
      property string buttonID = 'Nothing'

      you can not write QML inside js block

      declare your buttonID like you declare js variable , not QML property

        onPressed: {
              var buttonID = 'Nothing'
      
      1 Reply Last reply
      2
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #2

        Beside of a lot of syntax mistakes you are using switch in a wrong way. Add break at the end of each case block.

        1 Reply Last reply
        2
        • hslee_6560H hslee_6560

          Hi, I tried to use switch-case with mouse.button but, it seems that doesn't work.

          I want to know the reason.

          Why It doesn't work?

          MouseArea {
              anchors.fill: parent
              acceptedButtons: Qt.AllButtons
              // ...
              onPressed: {
                  property string buttonID = 'Nothing'
          
                  switch (mouse.button) {
                  case Qt.LeftButton: buttonID = 'LeftButton'; break;
                  case Qt.RIghtButton: buttonID = 'RIghtButton'; break;
                  }
                  test.text = 'pressed ' + buttonID
              }
          }
          

          I found it works whether I use if-else not switch-case.

          I can not understand what is the difference between two of them.

          Please, Let me know.

          Thank you! :)

          ps. I missed something like 'break' or 'acceptedButtons' ... but it were there when I found the problem, and I edited them. sorry.

          I am really sorry, It turned out my fault.
          I wrote wrong spell " switch (mouse.botton) { ..."
          now it works.
          Thanks all your comments.

          Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by Pradeep P N
          #3

          @hslee_6560 said in mouse.button with switch-case:

          switch (mouse.button) {
          case Qt.LeftButton: buttonID = 'LeftButton'
          case Qt.RIghtButton: buttonID = 'RIghtButton'
          }

          The acceptedButtons value for MouseArea is Qt.LeftButton

          To indicate that all possible mouse buttons are to be accepted, the special value 'Qt.AllButtons' may be used.
          Refer: acceptedButtons : Qt::MouseButtons.

          MouseArea { acceptedButtons: Qt.AllButtons }
          

          Please check for the Spelling & Syntax

              MouseArea {
                  property string buttonID: 'Nothing'
          
                  anchors.fill: parent
                  acceptedButtons: Qt.AllButtons
          
                  onClicked: {
                      switch (mouse.button) {
                      case Qt.LeftButton: buttonID = 'LeftButton'
                          break;
                      case Qt.RightButton: buttonID = 'RightButton'
                          break;
                      }
          
                      textEdit.text = 'pressed ' + buttonID;
                  }
              }
          

          All the best.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          1 Reply Last reply
          4
          • hslee_6560H hslee_6560

            Hi, I tried to use switch-case with mouse.button but, it seems that doesn't work.

            I want to know the reason.

            Why It doesn't work?

            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.AllButtons
                // ...
                onPressed: {
                    property string buttonID = 'Nothing'
            
                    switch (mouse.button) {
                    case Qt.LeftButton: buttonID = 'LeftButton'; break;
                    case Qt.RIghtButton: buttonID = 'RIghtButton'; break;
                    }
                    test.text = 'pressed ' + buttonID
                }
            }
            

            I found it works whether I use if-else not switch-case.

            I can not understand what is the difference between two of them.

            Please, Let me know.

            Thank you! :)

            ps. I missed something like 'break' or 'acceptedButtons' ... but it were there when I found the problem, and I edited them. sorry.

            I am really sorry, It turned out my fault.
            I wrote wrong spell " switch (mouse.botton) { ..."
            now it works.
            Thanks all your comments.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #4

            hi,
            @hslee_6560 said in mouse.button with switch-case:

            onPressed: {
            property string buttonID = 'Nothing'

            you can not write QML inside js block

            declare your buttonID like you declare js variable , not QML property

              onPressed: {
                    var buttonID = 'Nothing'
            
            1 Reply Last reply
            2

            • Login

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