Revision control
Copy as Markdown
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\example gallery
\ingroup canvaspainter-examples
\title Qt Canvas Painter - Gallery Example
\examplecategory {Graphics}
\brief Demonstrates the QCanvasPainter features in a Qt Quick application.
\image gallery-example.webp
{Brushes gallery titled Gradients and Image Patterns showing a
grid of gradient swatches and patterned tiles}
Gallery example is a Qt Quick application, showcasing a wide variety of
features offered by \l QCanvasPainter.
The user interface is essentially a \l ListView, where the delegate is \c
GalleryItem, a type implemented in C++ and exposed to QML.
\snippet gallery/galleryitem.h 0
With Qt 5 and OpenGL, GalleryItem could have been a subclass of \l
QQuickPaintedItem. With Qt 6 and Canvas Painter, we will subclass
QCanvasPainterItem, which provides accelerated rendering via \l QCanvasPainter,
instead of using plain software rendering of \l QQuickPaintedItem and \l
QPainter.
The GalleryItem implementation is simple. Reimplementing the virtual
function \l{QCanvasPainterItem::}{createItemRenderer()}, we create an
instance of GalleryItemRenderer, which subclasses \l QCanvasPainterItemRenderer.
\snippet gallery/galleryitem.cpp 0
The actual drawing is implemented in this class:
\snippet gallery/galleryitemrenderer.h 0
initializeResources() registers images, loaded via \l QImage, and shaders for the
custom brushes.
\snippet gallery/galleryitemrenderer.cpp initialize
synchronizeData() takes care of copying GalleryItem data so that it can be
safely accessed on the \l {Scene Graph and Rendering}{Qt Quick scene graph's
render thread} later on:
\snippet gallery/galleryitemrenderer.cpp synchronize
The paint() implementation renders using \l QCanvasPainter, depending on the current index
provided by the \l ListView:
\snippet gallery/galleryitemrenderer.cpp paint
The functions invoked from here use the \l QCanvasPainter API to perform the rendering. For example:
\snippet gallery/galleryitemrenderer.cpp draw
Check the full example source code linked below for details.
*/