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. Right button released event in MouseArea
Forum Updated to NodeBB v4.3 + New Features

Right button released event in MouseArea

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 483 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by
    #1

    Hello!
    I had a lot of whitespaces in QML understanding aspects, I have not found solutions, so I would like to ask someone who know.

    I need to catch right button released event while using MouseArea. It is easy to catch onClicked with the following code:

    onClicked: {
        if (mouse.buttons & Qt.RightButton) {
        doSomething();
        }
    }
    

    But when I am trying to do the same thing for onReleased event, mouse.buttons is zero for any mouse button released separately. It is non-zero if several buttons was pressed and released by order. So my question is how to detect only right-click event?

    ODБOïO 1 Reply Last reply
    0
    • J Offline
      J Offline
      jay1
      wrote on last edited by
      #3

      You can try the following to detect the Right mouse button click, it should be straight forward.

      Rectangle
      {
      	id: textContainerDisp
      	anchors.top: parent.top
      	width: parent.width
      	height: parent.height * 0.2
      	color: "#95232323"
      	Text {
      		id: imageTextDisp
      		anchors.horizontalCenter: textContainerDisp.horizontalCenter
      		anchors.centerIn: parent
      		text: "Initialized"
      		color: "#ffffff"
      		font.pixelSize: parent.height * 0.5
      	}
      
      	MouseArea
      	{
      		id: textContainerMouseArea
      		anchors.fill: parent
      		acceptedButtons: Qt.LeftButton | Qt.RightButton
      
      		onClicked:
      		{
      			if(mouse.button == Qt.RightButton)
      			{
      				textVar = "Mouse Clicked";
      				console.log("Click Event");
      			}
      		}
      
      		onPressed:
      		{
      			if(mouse.button == Qt.RightButton)
      			{
      				textVar = "Mouse Pressed";
      				console.log("Press Event");
      			}
      		}
      
      		onReleased:
      		{
      			if(mouse.button == Qt.RightButton)
      			{
      				textVar = "Mouse Released";
      				console.log("Release Event");
      			}
      		}
      	}
      }
      

      Here the sequence of the onPress/onRelease/onClicked events detected is as follows:

      qml: Press Event
      qml: Release Event
      qml: Click Event
      

      So here the "OnClicked" event is detected after the "OnRelease" event. Hope this answers your question.

      1 Reply Last reply
      2
      • S St.Stanislav

        Hello!
        I had a lot of whitespaces in QML understanding aspects, I have not found solutions, so I would like to ask someone who know.

        I need to catch right button released event while using MouseArea. It is easy to catch onClicked with the following code:

        onClicked: {
            if (mouse.buttons & Qt.RightButton) {
            doSomething();
            }
        }
        

        But when I am trying to do the same thing for onReleased event, mouse.buttons is zero for any mouse button released separately. It is non-zero if several buttons was pressed and released by order. So my question is how to detect only right-click event?

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

        @St-Stanislav hi

        i dont know if there is a direct solution,
        but you can do it like this

         property bool rClick: false
            Rectangle{
                height: 50
                width: 50
                color: "grey"
                MouseArea{
                    id: mouse
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
                    anchors.fill: parent
                    onPressed : {
                        if (mouse.buttons & Qt.RightButton) {
                            rClick = true
                        }
                    }
                    onReleased : if(rClick){
                                     console.log("right click released")
                                     rClick = false
                                 }
                }
            }
        
        S 1 Reply Last reply
        0
        • J Offline
          J Offline
          jay1
          wrote on last edited by
          #3

          You can try the following to detect the Right mouse button click, it should be straight forward.

          Rectangle
          {
          	id: textContainerDisp
          	anchors.top: parent.top
          	width: parent.width
          	height: parent.height * 0.2
          	color: "#95232323"
          	Text {
          		id: imageTextDisp
          		anchors.horizontalCenter: textContainerDisp.horizontalCenter
          		anchors.centerIn: parent
          		text: "Initialized"
          		color: "#ffffff"
          		font.pixelSize: parent.height * 0.5
          	}
          
          	MouseArea
          	{
          		id: textContainerMouseArea
          		anchors.fill: parent
          		acceptedButtons: Qt.LeftButton | Qt.RightButton
          
          		onClicked:
          		{
          			if(mouse.button == Qt.RightButton)
          			{
          				textVar = "Mouse Clicked";
          				console.log("Click Event");
          			}
          		}
          
          		onPressed:
          		{
          			if(mouse.button == Qt.RightButton)
          			{
          				textVar = "Mouse Pressed";
          				console.log("Press Event");
          			}
          		}
          
          		onReleased:
          		{
          			if(mouse.button == Qt.RightButton)
          			{
          				textVar = "Mouse Released";
          				console.log("Release Event");
          			}
          		}
          	}
          }
          

          Here the sequence of the onPress/onRelease/onClicked events detected is as follows:

          qml: Press Event
          qml: Release Event
          qml: Click Event
          

          So here the "OnClicked" event is detected after the "OnRelease" event. Hope this answers your question.

          1 Reply Last reply
          2
          • ODБOïO ODБOï

            @St-Stanislav hi

            i dont know if there is a direct solution,
            but you can do it like this

             property bool rClick: false
                Rectangle{
                    height: 50
                    width: 50
                    color: "grey"
                    MouseArea{
                        id: mouse
                        acceptedButtons: Qt.LeftButton | Qt.RightButton
                        anchors.fill: parent
                        onPressed : {
                            if (mouse.buttons & Qt.RightButton) {
                                rClick = true
                            }
                        }
                        onReleased : if(rClick){
                                         console.log("right click released")
                                         rClick = false
                                     }
                    }
                }
            
            S Offline
            S Offline
            St.Stanislav
            wrote on last edited by
            #4

            @LeLev, thanks for your reply! As I have tested a lot of combination seems like that this solutions will failed for the following sequence of actions:

            1. Right button pressed
            2. Left button pressed
            3. Left button released
            4. Right button released.

            For yours solution event will be triggered after 3rd point of the sequence.

            Looks like the @jay1 's solution will be correct, I will give a try to implement it. For some reason I have thought that mouse.buttons flags should be parsed not mouse.button value.

            ODБOïO 1 Reply Last reply
            1
            • S St.Stanislav

              @LeLev, thanks for your reply! As I have tested a lot of combination seems like that this solutions will failed for the following sequence of actions:

              1. Right button pressed
              2. Left button pressed
              3. Left button released
              4. Right button released.

              For yours solution event will be triggered after 3rd point of the sequence.

              Looks like the @jay1 's solution will be correct, I will give a try to implement it. For some reason I have thought that mouse.buttons flags should be parsed not mouse.button value.

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

              @St-Stanislav yes, you are right
              @jay1 's solution is actually the normal way to handle it ^^
              my bad

              1 Reply Last reply
              0

              • Login

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