How to change QScrollbar's background color without changing the handle's shape?

777 Views Asked by At

I am trying to change the background color of the scrollers in my QGraphicsView using stylesheet. But it also changes the handler's shape from a nice rounded bar to an ugly rectangle. Is it possible to retain the original shape? Here is the code:

QString style = R"( QScrollBar:vertical {
                            background: rgb(61,61,61);
                        }
                        QScrollBar::handle:vertical {
                            background: rgb(119,119,119);
                            min-width: 20px;
                        }

                     QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
                         background: none;
                     })";

myGraphicsView->setStyleSheet(style);

This is the original look:

Nice rounded handle

This is after setting the stylesheet (now the handle is rectangular):

ugly rectangular handle

1

There are 1 best solutions below

0
On

A bit late for the OP, but for others...

I would try adding more specific style for your handle.

I have the following in the application-level style-sheet that I managed to get working last year

QScrollBar:vertical {
border: 1px solid #5c5c5c; /* If this isn't specified... OMG */
/*background: yellow;*/
width: 50px;
margin: 35px 0 35px 0px;
}
QScrollBar::handle:vertical {
    /*background: green;*/
    min-height: 35px;
}

I suspect that with no style applied, the system default "native" style is used for the whole scrollbar. If you apply one element of style, then a (simple) default style is drawn explicitly, and your style is applied to that. So, if you only specify the background colour for your scrollbar, you'll get the default style for the handle. So, you want to specify most components of the handle to get it to look something like the native system one - border-style, color, border-radius...

I purged scroll bars from my app actually, and now I don't remember what happens if you don't specify the border - but it must have been pretty ugly ! (my aesthetic standards are minimal...)