pyqt5qframeeventfilter

How to set QFrame color in an eventFilter?


I have a simple QWidget that contains a frame and two labels. I want to use eventFilter to change QFrame background color on label hover. Can someone please check the below code and tell me why I can't change the QFrame background and if it is the correct way for doing it?

import sys

from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox, QFrame, QLabel
from PyQt5 import QtCore

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.layout = QHBoxLayout(self)

        self.frame = QFrame(self)
        self.setObjectName("frame")
        self.frame_lay = QHBoxLayout()

        self.one_label = QLabel(self.frame)
        self.one_label.setText("one")
        self.one_label.setObjectName("one")

        self.two_label = QLabel(self.frame)
        self.two_label.setText("two")
        self.two_label.setObjectName("two")

        self.one_label.installEventFilter(self)
        self.two_label.installEventFilter(self)

        self.frame_lay.addWidget(self.one_label)
        self.frame_lay.addWidget(self.two_label)

        self.frame.setStyleSheet("""QFrame{background-color: red;}""")

        self.frame.setLayout(self.frame_lay)
        self.frame_lay.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.frame)
        self.setLayout(self.layout)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.Enter:
            if type(obj) == QLabel:
                if obj.objectName() in ["one", "two"]:
                    print(obj.objectName())    
                    self.frame.setStyleSheet("""QFrame#frame{background-color: blue;}""")
        return super(QWidget, self).eventFilter(obj, event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Should the installEventFilter be applied to QWidget or QFrame? The labels are contained within the QFrame.

Thanks


Solution

  • You set the frame object name for the "MainWindow", but in the event filter you used the object name for a QFrame class.

    Just set the object name for the frame instead:

        self.frame.setObjectName("frame")
    

    Note that QLabel inherits from QFrame, so, using QFrame{background-color: red;} technically applies the background for both the parent frame and any child label.
    In case you want to be more specific, you either use the object name as you did in the event filter, or use the .ClassName selector, which applies the sheet only to the class and not its subclasses (note the full stop character before QFrame):

        self.frame.setStyleSheet(""".QFrame{background-color: red;}""")