QSS for QTabBar close button: visible on selected OR hovered
-
I am trying to customize the close button visibility on a QTabBar (used within QTabWidget with setTabsClosable(true)). My specific goal for the close button (QTabBar::close-button) is to make it visible only when:
The parent tab (QTabBar::tab) is currently selected (:selected). OR
The parent tab is not selected (:!selected), BUT the mouse cursor is currently hovering over that specific non-selected tab (:hover).
(this is style similar I saw in JetBrains IDEs)In all other cases (i.e., a non-selected tab that is not being hovered), the close button should be hidden.
I understand this requires styling the ::close-button subcontrol based on the state of the parent ::tab element. I've found that applying simple rules like image: none conditionally using selectors like QTabBar::tab:!selected QTabBar::close-button doesn't always seem reliable, potentially due to interactions with the underlying QStyle.
Could someone suggest the best or most reliable QSS approach to achieve this specific logic?
What QSS selectors and properties should I use to correctly show the close button on selected tabs and on non-selected tabs when they are hovered, while hiding it otherwise?
Crucially, is the proposed QSS solution likely to work consistently across both Qt 5 and common Qt 6 versions? Are there any known QSS interpretation differences or QStyle limitations between Qt5 and Qt6 that might affect this particular scenario?Any QSS examples or advice would be greatly appreciated!
Thanks in advance.
-
Hi,
I think that for this case, you should consider creating a QProxyStyle in order to be able to manage that hierarchy of constraints.