Change QMenu behavoir with QWidgtAction
-
Hello.
Well, I have implemented QMenu with QPushButton and then I created a QWidgetAction containing three different PushButtons and added it to my Menu, So far so good and it is working great.But now I want to have a different behaviour that the default one (left-click open menu, click on button)
Now I want -> User clicked on the menu (PushButton) the menu shows up (and still appear as long as he did not release the mouse) -> he keeps holding left-click and start hovering over the menu buttons -> And only when he releases the mouse over one of these buttons that button should be executed (if he releases the mouse over nothing then None of the buttons will be executed).
here is my implementation so far:
from PyQt5 import QtCore, QtGui from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QVBoxLayout, QApplication, QMainWindow, QFrame, QMenu, \ QWidgetAction class CustomQMenu(QMenu): def __init__(self, parent=None): super(CustomQMenu, self).__init__(parent) # .... Some stylesheet goes here ...... 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 = QPushButton() self.menu = CustomQMenu(self.open_menu) 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,
From your description you seem to reimplement the standard behavior of a QMenu with a set of action. Can you explain the need for these pusch buttons in place of the usual QActions ?
-
@SGaist Thank you a lot for your time.
Well, I have a big project and a tools container (QWidget contains buttons) with like 10 buttons (tools). Some of these buttons have a submenu (QMenu) that has buttons as well, And all of these buttons are related to one group, So only one button can be selected, Because am using QButtonGroup and as I remember 2 months ago when I searched for a way to add the default QMenu action into that QButtonGroup they suggested to use QWidgetAction instead and add the needed buttons.And we have a custom buttons style (pushbutton only has an icon and other stylesheets).
-
For that part, QActionGroup exists and while rereading this, it looks a bit like a QToolBar.
Would it be possible to have a drawing of what you want to implement ?
-
@SGaist. Am truly sorry I think I misexplained what I mean by 'tools' it's basically I have some kind of drawing shapes application and these buttons (tools) are like brush tool, delete tool ...etc.
Here is an image that will describe my application and the wanted idea.
-
Then I confirm, you can QTooBar to implement them.