How Can I add Right click functionality to PySide2 QpushButton?

Hi there.
I’m trying to call two different functions with left click and right click on a QPushButton.
I tried overriding “mousePressEvent” in my “QDockWidget” class with limited success.

def mousePressEvent(self, event):     
	super().mousePressEvent(event)
	if event.type() == QtCore.QEvent.MouseButtonPress:
		if event.button() == QtCore.Qt.RightButton:
			print ("RightButton ")
		elif event.button() == QtCore.Qt.LeftButton:
			print ("LeftButton" )

The QpushButtonCode is:

theButton5_tab1=QtWidgets.QPushButton(“RightClickTest”)
theButton5_tab1.clicked.connect(theButton5_tab1.clicked)

The problem is that when I right click function runs as expected and prints the string. But when I left click 3ds max crashes.

What exactly are you trying to achieve? Just an alternate functionality on left/right click of the same button?

Maybe something with an eventFilter instead?

(not python in that answer but the idea is the same)

Thank you I’ll try that.
I want to send mouse button argument to the “connect” called function.

For sure, if you don’t want to subclass, you could maybe try the filter or pass another function and check if the last button was a right click or something? Never tried to right click a pushbutton. Probably be easier to just check if it’s a shift or control click tbf

I solved it with “QtCore.Qt.CustomContextMenu”