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. How to transfer mouse focus back while showing a QMenu?
QtWS25 Last Chance

How to transfer mouse focus back while showing a QMenu?

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyside2qmenufocus
9 Posts 4 Posters 2.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.
  • T Offline
    T Offline
    ThePyGuy
    wrote on last edited by ThePyGuy
    #1

    I have two buttons and both of them has some hovering effect. The first button has a menu as well, and the problem is, when the first button is clicked and menu appears, the mouse hover doesn't work for the second button at the same time until the menu is closed.
    I'm not sure, but I believe it is due to some sort of focusPolicy, and I tried to find the solution but I couldn't. I just want to make hovering effect on the buttons of the widget available even while showing the menu. I'm not sure if the behavior I'm trying to achieve is possible or not, since QMenu takes the focus while showing because of being the active window
    Here is the minimal code to get started.

    from PySide2 import QtWidgets, QtGui, QtCore
    import sys
    
    class MyWidget(QtWidgets.QWidget):
        def __init__(self):
            super(MyWidget, self).__init__()
            self.resize(300, 300)
            layout = QtWidgets.QHBoxLayout(self)
    
            btn = QtWidgets.QPushButton('Button 1')
            btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
            layout.addWidget(btn)
            menu = QtWidgets.QMenu(self)
            action = QtWidgets.QAction('buttonAction', menu)
            menu.addAction(action)
            btn.setMenu(menu)
            
            btn = QtWidgets.QPushButton('Button 2')
            btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
            layout.addWidget(btn)
    
            self.setLayout(layout)
    
    app = QtWidgets.QApplication([])
    wig = MyWidget()
    wig.show()
    sys.exit(app.exec_())
    

    Please be noted, instead of stylesheet, I even tried using evenFilter; changing the colors on Enter/Leave events and returning True/False values.

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • T ThePyGuy

      I have two buttons and both of them has some hovering effect. The first button has a menu as well, and the problem is, when the first button is clicked and menu appears, the mouse hover doesn't work for the second button at the same time until the menu is closed.
      I'm not sure, but I believe it is due to some sort of focusPolicy, and I tried to find the solution but I couldn't. I just want to make hovering effect on the buttons of the widget available even while showing the menu. I'm not sure if the behavior I'm trying to achieve is possible or not, since QMenu takes the focus while showing because of being the active window
      Here is the minimal code to get started.

      from PySide2 import QtWidgets, QtGui, QtCore
      import sys
      
      class MyWidget(QtWidgets.QWidget):
          def __init__(self):
              super(MyWidget, self).__init__()
              self.resize(300, 300)
              layout = QtWidgets.QHBoxLayout(self)
      
              btn = QtWidgets.QPushButton('Button 1')
              btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
              layout.addWidget(btn)
              menu = QtWidgets.QMenu(self)
              action = QtWidgets.QAction('buttonAction', menu)
              menu.addAction(action)
              btn.setMenu(menu)
              
              btn = QtWidgets.QPushButton('Button 2')
              btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
              layout.addWidget(btn)
      
              self.setLayout(layout)
      
      app = QtWidgets.QApplication([])
      wig = MyWidget()
      wig.show()
      sys.exit(app.exec_())
      

      Please be noted, instead of stylesheet, I even tried using evenFilter; changing the colors on Enter/Leave events and returning True/False values.

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

      @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

      hover doesn't work for the second button at the same time until the menu is closed

      This is normal, why do you want to change it?

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

      T 1 Reply Last reply
      0
      • jsulmJ jsulm

        @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

        hover doesn't work for the second button at the same time until the menu is closed

        This is normal, why do you want to change it?

        T Offline
        T Offline
        ThePyGuy
        wrote on last edited by
        #3

        Hi @jsulm Thanks for the response!
        I want to change it because the widget has lots of QPushButtons and QLabels, and different buttons/labels have different hovering color, there are 8,9 categories and any button/label associated to any category has it's own hovering effect, so from UI perspective, it's becoming crucial to allow the hover effect even while showing the Menu.

        1 Reply Last reply
        0
        • T ThePyGuy

          I have two buttons and both of them has some hovering effect. The first button has a menu as well, and the problem is, when the first button is clicked and menu appears, the mouse hover doesn't work for the second button at the same time until the menu is closed.
          I'm not sure, but I believe it is due to some sort of focusPolicy, and I tried to find the solution but I couldn't. I just want to make hovering effect on the buttons of the widget available even while showing the menu. I'm not sure if the behavior I'm trying to achieve is possible or not, since QMenu takes the focus while showing because of being the active window
          Here is the minimal code to get started.

          from PySide2 import QtWidgets, QtGui, QtCore
          import sys
          
          class MyWidget(QtWidgets.QWidget):
              def __init__(self):
                  super(MyWidget, self).__init__()
                  self.resize(300, 300)
                  layout = QtWidgets.QHBoxLayout(self)
          
                  btn = QtWidgets.QPushButton('Button 1')
                  btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
                  layout.addWidget(btn)
                  menu = QtWidgets.QMenu(self)
                  action = QtWidgets.QAction('buttonAction', menu)
                  menu.addAction(action)
                  btn.setMenu(menu)
                  
                  btn = QtWidgets.QPushButton('Button 2')
                  btn.setStyleSheet('QPushButton::hover{background-color: gray;}')
                  layout.addWidget(btn)
          
                  self.setLayout(layout)
          
          app = QtWidgets.QApplication([])
          wig = MyWidget()
          wig.show()
          sys.exit(app.exec_())
          

          Please be noted, instead of stylesheet, I even tried using evenFilter; changing the colors on Enter/Leave events and returning True/False values.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

          I'm not sure if the behavior I'm trying to achieve is possible or not

          QMenu popups close themselves when they lose focus, so by default it's not possible to keep a popup visible while doing something else.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          T 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

            I'm not sure if the behavior I'm trying to achieve is possible or not

            QMenu popups close themselves when they lose focus, so by default it's not possible to keep a popup visible while doing something else.

            T Offline
            T Offline
            ThePyGuy
            wrote on last edited by
            #5

            @Pl45m4 I came to know it is not possible by default, but can this behavior be changed? If yes, can you point me to the right direction?

            Pl45m4P 1 Reply Last reply
            0
            • T ThePyGuy

              @Pl45m4 I came to know it is not possible by default, but can this behavior be changed? If yes, can you point me to the right direction?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              You should consider re-designing your GUI then. Open a new dialog, new widget or something else.

              @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

              I just want to make hovering effect on the buttons of the widget available even while showing the menu

              Of all buttons or just the clicked one, which is currently showing the QMenu?
              You could try a button which you paint gray as soon as you click it and take the stylesheet again, when you close the menu

              Signal before the menu closes:

              • https://doc.qt.io/qt-5/qmenu.html#aboutToHide

              Signal, before the button menu appears:

              • https://doc.qt.io/qt-5/qmenu.html#aboutToShow

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              T 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                You should consider re-designing your GUI then. Open a new dialog, new widget or something else.

                @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

                I just want to make hovering effect on the buttons of the widget available even while showing the menu

                Of all buttons or just the clicked one, which is currently showing the QMenu?
                You could try a button which you paint gray as soon as you click it and take the stylesheet again, when you close the menu

                Signal before the menu closes:

                • https://doc.qt.io/qt-5/qmenu.html#aboutToHide

                Signal, before the button menu appears:

                • https://doc.qt.io/qt-5/qmenu.html#aboutToShow
                T Offline
                T Offline
                ThePyGuy
                wrote on last edited by
                #7

                @Pl45m4 Thanks for your response, Yes, aboutToShow and aboutToHide can be used to change the background color of the clicked button immediately even if the menu was being shown for a button, for e.g.: clicking another button will close the current menu, aboutToHide of this closed menu will reset the stylesheet for the button for which menu was being shown, finally aboutToShow will change the stylesheet for newly clicked button before showing the corresponding menu.

                However, I'm actually concerned about the hovering effect, and the thing you suggested is the last option if what I'm trying to achieve is something that is not doable.

                Pl45m4P mrjjM 2 Replies Last reply
                0
                • T ThePyGuy

                  @Pl45m4 Thanks for your response, Yes, aboutToShow and aboutToHide can be used to change the background color of the clicked button immediately even if the menu was being shown for a button, for e.g.: clicking another button will close the current menu, aboutToHide of this closed menu will reset the stylesheet for the button for which menu was being shown, finally aboutToShow will change the stylesheet for newly clicked button before showing the corresponding menu.

                  However, I'm actually concerned about the hovering effect, and the thing you suggested is the last option if what I'm trying to achieve is something that is not doable.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @ThePyGuy said in How to transfer mouse focus back while showing a QMenu?:

                  However, I'm actually concerned about the hovering effect

                  Why should there be a hovering effect, when you actually don't hover over your button or while the menu still has focus?
                  Maybe you provide an image to show what exactly you are trying to do and why, because I still don't understand why one would need this. This is not intuitive nor expected behavior .


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  1
                  • T ThePyGuy

                    @Pl45m4 Thanks for your response, Yes, aboutToShow and aboutToHide can be used to change the background color of the clicked button immediately even if the menu was being shown for a button, for e.g.: clicking another button will close the current menu, aboutToHide of this closed menu will reset the stylesheet for the button for which menu was being shown, finally aboutToShow will change the stylesheet for newly clicked button before showing the corresponding menu.

                    However, I'm actually concerned about the hovering effect, and the thing you suggested is the last option if what I'm trying to achieve is something that is not doable.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @ThePyGuy

                    Hi
                    Im not sure its possible as QMenu get all the events when open.

                    https://forum.qt.io/topic/34557/solved-prevent-qmenu-from-being-modal

                    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