why does qsplitterhandles refuse to format my messages?

Premise

QSplitterHandle is one of the few very specific widgets that don’t seem to follow generic QWidget “rules”; the reason is pretty simple: its aim is normally limited to the usage it’s intended for, which is being a QSplitter child.
Then, you have to consider that, while pretty “liberal”, the stylesheet syntax follows basic but important rules, it depends on the underlying QStyle in use and its behavior might change whenever a child widget is very dependant on its parent, as in this case.


First of all, there’s a conceptual problem in your approach: the stylesheet syntax is wrong, as you’re trying to use a selector-like syntax (using {braces}) without specifying a selector. With a syntax like that, the stylesheet won’t be applied anyway, even with much more “standard” widgets.

But that’s not enough. The QSplitter handle() documentation explains an important point (emphasis mine):

Returns the handle to the left of (or above) the item in the splitter’s layout at the given index, or nullptr if there is no such item. The handle at index 0 is always hidden.

Even if your syntax were correct, it wouldn’t have worked anyway since you tried to apply the stylesheet to the first (hidden) handle.

So, theoretically, the correct syntax should’ve been the following:

self.splitter.handle(1).setStyleSheet("background-color: red;")

even better:

self.splitter.handle(1).setStyleSheet(
    "QSplitterHandle {background-color: red;}")

or, for all handles:

self.splitter.setStyleSheet(
    "QSplitterHandle {background-color: red;}")

On the other hand, since QSplitterHandle is a very peculiar widget, its color properties are applied in different ways according to the currently applied QStyle, even when a correct syntax is used. Remember that stylesheets are overrides to the current QStyle, which means that the QStyles states the painting rules, and applies the eventual stylesheet according to those rules.
For instance, while the “windows” style uses the background color rule of the stylesheet to draw the actual background of the handle, the fusion style uses the background color to draw the “dots” of the splitter only.

As with other complex widgets (like QComboBox or QAbstractItemView), you need to use specific selectors for sub-controls, as clearly explained in the QSplitter stylesheet documentation; using the selector you ensure that the color rule is applied to the handle as a whole, no matter the style in use:

self.splitter.setStyleSheet("QSplitter::handle {background-color: red}")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top