简体   繁体   中英

Qt QScrollBar change background Color

I was working on Qt Designer and I wanted to edit the background color (if possible a gradient) of QScrollBar but I don't know how to do it properly.

When I add any background color the whole widget changes color uniformly and becomes solid.

I was trying to change the color of the area behind the bar and arrow buttons only.

Unfortunately, with many complex widgets you can't just change a property with stylesheet without all the necessary implementation.

QScrollBar is one of those widgets, and in order to achieve background customization through stylesheets, at least all the following have to be provided too:

  • overall background color
  • add/sub-page color
  • handle color
  • add/sub-line ("arrow buttons") colors

To make the widget more visually responsive, borders (with pseudo-states for pressed/enabled/hover states) should also be provided, and adding images for arrow buttons is usually suggested (small arrows can be obtained through css border tricks, though).

The starting point is the official Qt stylesheet example documentation, which has a section for customizing QScrollBar .

Note that using a QProxyStyle is not an option, as many styles draw scrollbars in different ways, and sometimes they even ignore some "official" style functions like drawControl, because they take care of the whole painting within the drawComplexControl, using private functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM