1

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

Thinium
  • 171
  • 1
  • 14
  • 1
    Try by inheriting the qstyle that you used to style your application and then change the palette. In this [repository](https://github.com/Jorgen-VikingGod/Qt-Frameless-Window-DarkStyle) you can see a full working example on how to change the colors used starting from a given style by only changing the palete. Take a look at [DarkStyle.h](https://github.com/Jorgen-VikingGod/Qt-Frameless-Window-DarkStyle/blob/master/DarkStyle.h) and [DarkStyle.cpp](https://github.com/Jorgen-VikingGod/Qt-Frameless-Window-DarkStyle/blob/master/DarkStyle.cpp). – apalomer Apr 04 '20 at 15:26
  • If you want to override the palette for only one type of widgets, you can overload `void QStyle::polish(QWidget *w)` and in the function check with a qobject_cast if it is the desired type of widget, then change the palette and call the implementation of your base-class style. – apalomer Apr 04 '20 at 15:31
  • @apalomer I am able to find the scrollbar, but what parameters should I change in order to change the background color and the handle's color? void MyQProxyStyle::polish(QWidget* widget) { QScrollBar* wig = qobject_cast(widget); if (wig != nullptr) { qDebug() << "found a scrollbar"; QPalette palette; palette.setColor(QPalette::Base, QColor(119, 119, 119)); palette.setColor(QPalette::AlternateBase, QColor(61, 61, 61)); palette.setColor(QPalette::Light, QColor(119, 119, 119)); wig->setPalette(palette); } } – Thinium Apr 04 '20 at 17:24
  • can you share your style? I'm afraid this will be a combination of stylesheet and style maybe. – apalomer Apr 04 '20 at 18:10
  • @apalomer, I ended up using your DarkStyle class and the darkstyle.qss. It works for the scroll bar, thanks :). But the strange thing is that the button background remains white in Light mode on mac. – Thinium Apr 04 '20 at 18:33
  • Just to clarify, it is not my work. – apalomer Apr 04 '20 at 18:43

0 Answers0