Bokambo
Bokambo

Reputation: 4480

QListView Transparent Scrollbar with Image in QT

I am having QListWidget, i want to have a Transparent Scrollbar with Image. Initially that scrollbar should be hidden on scroll only it should show. How i Can achieve this in Qt ? Any examples or ideas are welcomed. How i can apply image to Listview Scrollbar.

Upvotes: 0

Views: 1507

Answers (1)

phyatt
phyatt

Reputation: 19152

Here are the three things you can look at to be able to control the scroll bar for most kinds of widgets in Qt:

To be able to track how the user interacts with your QListWidget or any Widget for that matter you need to subclass it and implement the virtual methods from the QWheelEvent and possibly the QKeyEvent.

Scrolling is typically done with the mouse wheel and with the keyboard arrow keys and sometimes page-up and page-down and spacebar. I haven't done a lot with QListWidget, but you should double check which keyboard events/mouse events trigger scrolling.

These events will cause scrolling event even after you set either or both of the ScrollBarPolicies for the widget to be Qt::ScrollBarAlwaysOff.

First you should put in the constructor of your widget

    this->setVerticalScrollBar(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBar(Qt::ScrollBarAlwaysOff);

So you just need to setMouseTracking(true) for the widget (so that it tracks more than just the clicks) and reimplement at the very least wheelEvent(), and when a wheel event occurs, set the vertical/horizontal scroll bar policies to true and call update on your widget.

If you want to turn the scrollbars back off after a few milliseconds after they have started scrolling, you will need to create a QTimer in your constructor for your subclassed widget and connect it to a slot that sets the scroll bar polices on the timeout. Then you start/restart that timer every time the user does a wheelEvent().

As far as applying an image to the ListView Scrollbar, you should look into subclassing QAbstractScrollBar, if you want to actually put an image on it or change the way it looks. Setting up some tool buttons may also be the way to go if you are trying to put buttons with different icons in place of the scrollbar.

Upvotes: 1

Related Questions