C

Qt Quick Ultralite image_loading Example

Demonstrates how to load images at runtime.

Overview

The example shows how to use Qul::ImageProvider and Qul::Image to create and display an image at runtime.

The image of the striped flag in the middle of the screen is created with a random coloring when the Load new! button is pressed.

Target platforms

Project structure

CMake project file

Set the FreeRTOS configTOTAL_HEAP_SIZE variable to a value that is sufficient to store the image on the heap.

if (OS STREQUAL "FreeRTOS")
    math(EXPR heap_size "1024 * 1024")
    add_compile_definitions(configTOTAL_HEAP_SIZE=${heap_size})
endif()

The application must register a custom image provider. This is done in the main() function, which differs between BareMetal and FreeRTOS:

if (OS STREQUAL "FreeRTOS")
    target_sources(image_loading PRIVATE
                   main_freertos.cpp
    )
else()
    target_sources(image_loading PRIVATE
                   main_baremetal.cpp
    )
endif()
Application UI

The image_loading.qml file defines the user interface.

The dynamic image is shown using an Image item with its source property set to the image provider URI.

        Image {
            anchors.horizontalCenter: parent.horizontalCenter

            // Trigger a new image to be loaded when root.imageName changes
            source: "image://myimageprovider/" + root.imageName
            width: 120
            height: 120
        }
Image provider

The MyImageProvider image provider is implemented in myimageloader.h and myimageloader.cpp.

The MyImageProvider::requestImage() method begins by parsing the image URI, allocating a new Qul::Image, and preparing it for writing:

Qul::SharedImage MyImageProvider::requestImage(const char *imageName, size_t imageNameLength)
{
    if (imageNameLength < 8)
        return {};

    // Parse the image name to determine the colors to use for the image
    const int colors[3] = {imageName[5] - '0', imageName[6] - '0', imageName[7] - '0'};

    // Create the image and mark it as being-written-to
    Qul::Image image(120, 120, Qul::PixelFormat_RGB16);
    Qul::SharedImage sharedImage(image);
    image.beginWrite();

It starts the write operation that updates the image over time. In the example, this is done through a timer. In practice, an application can either launch an asynchronous process to fetch and decode an image or perform drawing operations on the image.

When the write operation completes, an event is sent to Qul::EventQueue, notifying the image as ready.

void MyImageProvider::onEvent(const MyImageLoadedEvent &event)
{
    // Causes the finished image to become visible.
    event.sharedImage.image()->endWrite();
}

The image provider is registered with the application in the main() function.

int main()
{
    MyImageProvider myImageProvider;
    Qul::Application app = {};
    app.addImageProvider("myimageprovider", &myImageProvider);
    ...

Files:

See also Qul::Image, Qul::SharedImage, and Qul::ImageProvider.

Available under certain Qt licenses.
Find out more.