Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMenu opens only while holding left-click
QtWS25 Last Chance

QMenu opens only while holding left-click

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.0k Views
  • 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
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • P Pythonic person

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      P 1 Reply Last reply
      1
      • JonBJ JonB

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

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

        @JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • P Pythonic person

          @JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Pythonic person

            @JonB Thanks for the fast answer, but sorry what do you mean by mouse up/down?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Pythonic-person
            QWidget::mousePressEvent(), QWidget::mouseReleaseEvent(). They seem to call it press/release.

            1 Reply Last reply
            1
            • P Offline
              P Offline
              Pythonic person
              wrote on last edited by
              #6

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

                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.

                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
                1
                • SGaistS SGaist

                  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.

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

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

                  M 1 Reply Last reply
                  0
                  • P Pythonic person

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

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #9

                    @Pythonic-person
                    Wy not using a QToolButton with mode set to QToolButton::InstantPopup.

                    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