Busy-Indicator-for-QML

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

Busy Indicator

Introduction

Certain graphical assets may take a while to load or you may wish to show that some other processing is going on. This custom BusyIndicator shows one way in which visual feedback can be provided. This busy indicator has been implemented as a custom QDeclarativeItem in C++ since it uses a conical gradient which it is not possible to represent in an SVG (which only have support for linear and radial gradients). We do take care to minimise the amount of expensive imperative drawing operations. My inspiration for this design comes from StarCraft 2 ;-)

Busy Indicator

Implementation

First, here is the class declaration:

This is quite a simple sub-class of QDeclarativeItem with only a handful of properties for setting the inner and outer radii of the busy indicator’s ring as a fraction of the item’s size. In this case the item’s size is defined to be min( width, height ) so as to preserve the 1:1 aspect ratio of the ring.

Now for the implementation:

In the constructor we set up a default size of 100×100 pixels for the indicator and call the updateSpinner() function. This function is also called whenever one of the affecting properties changes. These are:

  • Height
  • Width
  • Inner radius
  • Outer radius
  • Background color
  • Foreground color

The implementation of the updateSpinner() function only calculates a new QString value which is later used in paint() as a key in the global QPixmapCache. In the paint() function we check to see if the QPixmapCache already contains a matching pixmap or not. If it does we paint it. If it does not we first generate it, store it in the cache and then paint it.

This approach minimises the amount of expensive painting calls and key constructions.

Usage

Before we can use our custom item in any QML scene we need to expose it to the QML world. We do this with something along these lines:

Then in your QML scene you need to instruct the QML backend to import this collection (of 1) components with:

You are now ready to roll.

Independent Usage

The above should provide you with a nicely spinning busy indicator. Obviously the size and colors can be varied using the properties we declared in the header file.

Compound Usage Within Another Component

It is also easy to include the BusyIndicator into compound components. One example might be for slow to load images:

This will show a nice spinning busy indicator until the image is loaded. Of course you can expose more of the properties to the outside world if you like – this is just a simple example after all.

Another example using this Busy Indicator component along with a small progress bar in the center of the spinning ring to show loading progress for e.g. is shown in this snippet [developer.qt.nokia.com].

Independent Usage as Widget

This implementation of a busy indicator can also be used without QML. It can be added as widget through QGraphicsView [developer.qt.nokia.com] and QGraphicsScene [developer.qt.nokia.com] to a layout [developer.qt.nokia.com] and animated with QTimeLine [developer.qt.nokia.com] as shown at the following example. It is important to note that the viewport must be set in order to display the busy indicator.

Example

  • pro file
  • mainwindow.h
  • mainwindow.cpp

Categories: