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

Right button released event in MouseArea



  • 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?



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



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


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



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



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


Log in to reply