QML Performance

Opaque Items

Items hidden behind an opaque item incur a cost. If an item will be enitrely obscured by an opaque item, set its opacity to 0. One common example of this is when a "details" page is shown over the main application view.

Clipping

clip is set to false by default. Enable clipping only when necessary.

Anchors vs. Binding

It is more efficient to use anchors rather than bindings to position items relative to each other. Consider this use of bindings to position rect2 relative to rect1:

  1. Rectangle  {
  2.     id: rect1
  3.     x: 20
  4.     width: 200; height: 200
  5. }
  6. Rectangle  {
  7.     id: rect2
  8.     x: rect1.x
  9.     y: rect1.y + rect1.height
  10.     width: rect1.width - 20
  11.     height: 200
  12. }

This is achieved more efficiently using anchors:

  1. Rectangle  {
  2.     id: rect1
  3.     x: 20
  4.     width: 200; height: 200
  5. }
  6. Rectangle  {
  7.     id: rect2
  8.     height: 200
  9.     anchors.left: rect1.left
  10.     anchors.top: rect1.bottom
  11.     anchors.right: rect1.right
  12.     anchors.rightMargin: 20
  13. }

Images

Images consume a great deal of memory and may also be costly to load. In order to deal with large images efficiently it is recommended that the Image::sourceSize property be set to a size no greater than that necessary to render it. Beware that changing the sourceSize will cause the image to be reloaded.

Images on the local filesystem are usually loaded synchronously. This is usually the desired behavior for user interface elements, however for large images that do not necessarily need to be visible immediately, set the Image::asynchronous property to true. This will load the image in a low priority thread.

View Delegates

Delegates must be created quickly as the view is flicked. There are two important aspects to maintaining a smooth view:

  • Small delegates - keep the amount of QML to a minimum. Have just enough QML in the delegate to display the necessary information. Any additional functionality that is only needed when the delegate is clicked, for example, should be created by a Loader as needed.
  • Fast data access - ensure the data model is as fast as possible.

Image resources over composition

If possible, provide a single image resource, rather than using composition of a number of elements. For example, a frame with a shadow could be created using a Rectangle placed over an Image providing the shadow. It is more efficient to provide an image that includes the frame and the shadow.

Limit JavaScript

Avoid running JavaScript during animation. For example, running a complex JavaScript expression for each frame of an x property animation.

Rendering

Often using a different graphics system will give superior performance to the native graphics system (this is especially the case on X11). This can be configured using QApplication::setGraphicsSystem() or via the command line using the -graphicssystem switch.

You can enable OpenGL acceleration using the opengl graphics system, or by setting a QGLWidget as the viewport of your QDeclarativeView.

You may need to try various options to find what works the best for your application. For embedded X11-based devices one recommended combination is to use the raster graphics system with a QGLWidget for the viewport. While this doesn't guarantee the fastest performance for all use-cases, it typically has consistently good performance for all use-cases. In contrast, only using the raster paint engine may result in very good performance for parts of your application and very poor performance elsewhere.

The QML Viewer uses the raster graphics system by default for X11 and OS X. It also includes a -opengl command line option which sets a QGLWidget as the viewport of the view. On OS X, a QGLWidget is always used.

You can also prevent QDeclarativeView from painting its window background if you will provide the background of your application using QML, e.g.

  1.                 QDeclarativeView window;
  2. window.setAttribute(Qt::WA_OpaquePaintEvent);
  3. window.setAttribute(Qt::WA_NoSystemBackground);
  4. window.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
  5. window.viewport()->setAttribute(Qt::WA_NoSystemBackground);
Notes provided by the Qt Community

No notes