How to style a QML scroll bar with an image provider

From Qt Wiki
Revision as of 15:49, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How to style a QML scroll bar with an image provider

This is not a recommended way to style QML elements, but it does provide an example of
creating a QML plugin and creating an image provider. In this example, some parameters
have been tailored for Windows XP.

main.qml

This is a simple UI to have something which needs a scroll bar. The ScrollBar element is in the ScrollBar.qml below. It is connected to a ListView element by setting the target property to the id of the list view. The list view is filled with 100 items to give it something to scroll.

ScrollBar.qml

This ScrollBar is originally by Gregory Schlomoff [bitbucket.org]. It was modified from the original by changing the source property for Image and BorderImage elements from a image file reference to an image provider reference of the form “image://style/control/subControl”, where control and subControl are replaced with names of those QStyle parts. What values those can be are defined in the StyleImageProvider.cpp below. In this example, the only valid values are scrollBar and the subControls of a scroll bar.

qmldir

This defines the plugin name and resources for it.

StylePlugin.h

StylePlugin.cpp

StyleImageProvider.h

The StyleImageProvider class parses the requested pixmap’s id for the control and subControl (and an optional state, hover, pressed, etc. which is not used in this example). A QStyleOptionComplex is created and the QStyle from the application is used to paint into a pixmap. The pixmap is cropped to an appropriate size and returned.

StyleImageProvider.cpp

style.pro

Categories: