I am using PyQt and I want to add a right click to a widget, but I can't find any code on this subject online.
How can you do it ?
How to get a right click menu from a QTable in PyQt. To be able to have a right click menu in a QTable (PyQt), you need to follow the following few steps. Turn on Custom Context Menu. In QtDesigner, and in the property editor select in the contextMenuPolicy the option CustomContextMenu. Connect Custom Context Menu Signal. Catch Signal in Main Code
PyQt buttons. Buttons (QPushButton) can be added to any window. The QPushButton class has the method setText() for its label and move(x,y) for the position. In this article you can see how a button can be added to a window, and how you can connect methods to it. Related Course: PyQt Desktop Apps with Python.
PyQt5 - Basic Widgets Sr.No Widgets & Description 1 QLabel A QLabel object acts as a placeho ... 2 QLineEdit QLineEdit object is the most c ... 3 QPushButton In PyQt API, the QPushButton ... 4 QRadioButton A QRadioButton class object ... 17 more rows ...
Since QVBoxLayout is another box layout, its .addWidget () method works the same as in QHBoxLayout. Here’s a PyQt application that shows how to create and use a QVBoxLayout object for creating vertical arrangements of widgets in your GUIs: On line 16, you create an instance of QVBoxLayout. On lines 18 to 20, you add three buttons to layout.
You just have to override the methods that take care of it.
In this case you will override the mousePressEvent, have a look on this and see if it makes sense and works for what you need.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
def mousePressEvent(self, QMouseEvent):
if QMouseEvent.button() == Qt.LeftButton:
print("Left Button Clicked")
elif QMouseEvent.button() == Qt.RightButton:
#do what you want here
print("Right Button Clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MyWidget()
mw.show()
sys.exit(app.exec_())
Another good way to do that would be installing a event filter in your object and overriding its eventFilter. Inside that method you would make what you want. Remember you can always make use of pyqtSignal for good practices and call another object to make the job, not overloading the method with a lot of logic.
Here is another small example:
import sys
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.installEventFilter(self)
def eventFilter(self, QObject, event):
if event.type() == QEvent.MouseButtonPress:
if event.button() == Qt.RightButton:
print("Right button clicked")
return False
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MyWidget()
mw.show()
sys.exit(app.exec_())
Note: Remember that this last example will receive ALL KIND OF EVENTS, so you will have to be careful and make sure it's the one you want and not runtime breaking your app calling methods of your event that doesn't exist because it's not of that kind. For example if you call event.button()
without making sure before that it is a QEvent.MouseButtonPress
your app would break of course.
There are other ways to do that, these are the most known ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With