Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to transfer mouse focus back while showing a QMenu?



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


  • Lifetime Qt Champion

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



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



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



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



  • 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:

    Signal, before the button menu appears:



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



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


  • Lifetime Qt Champion

    @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