Details
-
Bug
-
Resolution: Out of scope
-
Not Evaluated
-
None
-
6.4.0.1
-
None
-
Windows 11
Description
Hello. I have a QLabel nested in a QFrame.
When the frame has focus, I can change its background, but not the color of the QLabel:
QFrame:focus QLabel { color: red; }
does not work. Or more precisely, it works too well, because it applies even if the QFrame does not have focus.
Here is a small non-working example showing the problem:
from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication, QFrame, QLabel, QVBoxLayout, QWidget class FocusFrame(QFrame): def __init__(self): super().__init__() self.label = QLabel("Focus on the Frame changes my color!", self) self.label.setAttribute(Qt.WA_TransparentForMouseEvents) layout = QVBoxLayout() layout.addWidget(self.label) self.setLayout(layout) self.setStyleSheet( """ QFrame { background-color: lightgray; } QFrame:focus { background-color: blue; } QFrame QLabel { color: black; } QFrame:focus QLabel { color: red; /* Color of QLabel changes to red when QFrame has focus */ } """ ) self.setFocusPolicy(Qt.StrongFocus) def focusInEvent(self, event): self.update() super().focusInEvent(event) def focusOutEvent(self, event): self.update() super().focusOutEvent(event) class MainWindow(QWidget): def __init__(self): super().__init__() self.frame = FocusFrame() self.frame2 = FocusFrame() main_layout = QVBoxLayout() main_layout.addWidget(self.frame) main_layout.addWidget(self.frame2) self.setLayout(main_layout) self.setWindowTitle("Focus-sensitive QLabel in QFrame") self.resize(300, 100) app = QApplication([]) window = MainWindow() window.show() app.exec()
-> the qlabel is never black, but should when no focus on its parent.
This a problematic, and I couldn't find any other way to get around this.