QMenu opens only while holding left-click
-
Hello, I have a QMenu and am trying to have this kind of behaviour which is the following:
Click -> open the menu.
Release -> hide the menu.How can I achieve that?
-
Hello, I have a QMenu and am trying to have this kind of behaviour which is the following:
Click -> open the menu.
Release -> hide the menu.How can I achieve that?
@Pythonic-person
That is not click to show/open, that is mouse down. With click it would already have been released. So why not show it on mouse down and hide it on mouse up? -
@Pythonic-person
That is not click to show/open, that is mouse down. With click it would already have been released. So why not show it on mouse down and hide it on mouse up?@JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?
-
@JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?
@Pythonic-person said in QMenu opens only while holding left-click:
but sorry what do you mean by mouse up/down?
Well, pressing mouse button down and the releasing it...
-
@JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?
@Pythonic-person
QWidget::mousePressEvent(), QWidget::mouseReleaseEvent(). They seem to call it press/release. -
Well, i have tried that already, But I am confused about how the QMenu works, because when I run the following code, as you can see I overrode all functions related to 'show' and even the menu still appears. Moreover, the printed messages are really weird -> when you click on the "open" button it prints "released" Why it didn't take the mousePressEvent?
Please if there is any tutorial or docmutionas that I've missed let me know, because I search a lot with no results.
This code is just for demonstrating my idea,
from PyQt5 import QtCore, QtGui from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QVBoxLayout, QApplication, QMainWindow, QFrame, QMenu, \ QWidgetAction class test(QPushButton): def showMenu(self): super(test, self).showMenu() def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None: super(test, self).mousePressEvent(a0) class CustomQMenu(QMenu): def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None: print("Press") def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None: print("release") def mouseDoubleClickEvent(self, a0: QtGui.QMouseEvent) -> None: print("double-click") def show(self) -> None: print("show") def aboutToShow(self) -> None: print("aboutToShow") def showEvent(self, a0: QtGui.QShowEvent) -> None: print("showEvent") def showFullScreen(self) -> None: print("showFull") def showMaximized(self) -> None: print("max") def showMinimized(self) -> None: print("Min") def showNormal(self) -> None: print("normal") def showTearOffMenu(self) -> None: print("teanOff") class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QHBoxLayout(self.centralwidget) self.horizontalLayout.setObjectName("horizontalLayout") self.open_menu = test() self.open_menu.setMaximumSize(100, 100) self.menu = CustomQMenu(self.open_menu) self.open_menu.clicked.connect(self.foo) self.open_menu.setMenu(self.menu) self.child_1 = QPushButton() self.child_1.clicked.connect(self.foo) self.child_2 = QPushButton() self.child_2.clicked.connect(self.foo) self.child_3 = QPushButton() self.child_3.clicked.connect(self.foo) action_box = QWidgetAction(self.menu) container = QWidget() layout = QVBoxLayout(container) layout.addWidget(self.child_1) layout.addWidget(self.child_2) layout.addWidget(self.child_3) action_box.setDefaultWidget(container) self.menu.addAction(action_box) self.horizontalLayout.addWidget(self.open_menu) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def foo(self): print("This is from child") self.menu.hide() def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.open_menu.setText("Open") self.child_1.setText("child_1") self.child_2.setText("child_2") self.child_3.setText("child_3") if __name__ == "__main__": import sys app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
-
Hi,
The main issue you have here is that you do not call the base class implementations of these methods and thus you do not allow your class to operate correctly.
-
Hi,
The main issue you have here is that you do not call the base class implementations of these methods and thus you do not allow your class to operate correctly.
@SGaist, Thanks for the reply.
Yes, you are right, but even when I call the base class still I have the same output (I mean the printed messages) as I can see, when you click on that QPushButton (open menu button) it will print on console -> it prints: 'showEvent' and 'release' so where is the 'press'?So the **mousePressEvent has not been called while opening the menu whereas it has been called after the menu gets open.
The only method that has been called while opining the menu is: showEvent() then mouseReleaseEvent()
-
@SGaist, Thanks for the reply.
Yes, you are right, but even when I call the base class still I have the same output (I mean the printed messages) as I can see, when you click on that QPushButton (open menu button) it will print on console -> it prints: 'showEvent' and 'release' so where is the 'press'?So the **mousePressEvent has not been called while opening the menu whereas it has been called after the menu gets open.
The only method that has been called while opining the menu is: showEvent() then mouseReleaseEvent()
@Pythonic-person
Wy not using a QToolButton with mode set to QToolButton::InstantPopup.