Hide QScrollBar arrows using stylesheets

64 Views Asked by At

I am trying to get rid of the arrows of a vertical QScrollBar as part of a QListWidget by applying a stylesheet using Qt 5.15.

As I don't want weird behavior at the top and bottom, simply hiding the arrows as suggested here is insufficient. One answer to this question suggests to set the width and height of the arrows to 0px, which I tried using

setStyleSheet("QScrollBar:vertical::up-arrow, QScrollBar:vertical::down-arrow {height: 0px;}");

but this results in the scrollbar not showing up. Forcing it to show up by using

setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

on the QListWidget results in the standard scrollbar to show up.

Interestingly enough, using any non-zero height, e.g. 30px leads to the same results while changing the width works perfectly fine (although the width is then applied to the whole scrollbar but that is beside the point).

How can I achieve this without rewriting all mouseEvents?

1

There are 1 best solutions below

0
On

musicamante said:

Note that when setting one property or subcontrol for a complex widget like QScrollBar, you must set all the other properties too (see the documentation). At least you need to also set the properties for the : handle and the :groove. Check the examples for QScrollBar customization.

Based on that I found out that I had the wrong idea about add-line and sub-line as well as the arrows.

The following code managed to hide the arrows:

setStyleSheet("QScrollBar:vertical { background: transparent; width: 10px; margin: 0px 0px 0px 0px; }"
              "QScrollBar::add-line:vertical { border: 0px; height: 0px; }"
              "QScrollBar::sub-line:vertical { border: 0px; height: 0px; }");

Setting the width of QScrollBar:vertical is optional but changing the background color appears to be required.