Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Change QMenu behavoir with QWidgtAction
Forum Updated to NodeBB v4.3 + New Features

Change QMenu behavoir with QWidgtAction

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 591 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pythonic person
    wrote on last edited by Pythonic person
    #1

    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_())
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        P Offline
        P Offline
        Pythonic person
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            P Offline
            P Offline
            Pythonic person
            wrote on last edited by
            #5

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

            menu.png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then I confirm, you can QTooBar to implement them.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved