Thursday, 12 August 2021

How to transfer mouse focus back while showing a QMenu?

I have two buttons and both of the 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.

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.



from How to transfer mouse focus back while showing a QMenu?

No comments:

Post a Comment