本页内容

C

Qt Quick Ultralite 涂装物品示例

演示如何在Qt Quick Ultralite应用程序中使用PaintedItem 。

概述

该painteditem 示例使用PaintedItem QML类型直接在帧缓冲区上绘制。它展示了一段动画,模拟了圆形斑点在弯曲斜坡上的振荡运动。

该示例使用了 OscillatorPaintedItem 类,该类重写了paint() 方法以绘制一条弯曲斜坡和一个圆形斑点。每当 angle 属性发生更新事件时,都会调用paint() 方法,而该属性每 100 毫秒更新一次。

PaintedItem 示例,展示了一个位于弯曲坡道上的圆,左上角显示角度和阻尼系数值,并带有“重播”按钮。

目标平台

项目结构

painteditem 示例由以下文件组成:

  • CMakeLists.txt
  • mcu_painteditem.qmlproject
  • oscillator.qml
  • main_baremetal.cpp
  • oscPaintedItem.cpp
  • oscPaintedItem.h

CMake 项目文件包含一个基本的构建脚本oscillator.qml ,该脚本定义了应用程序的用户界面,而项目配置则在 mcu_painteditem.qmlproject 中定义。

main_baremetal.cpp 包含用于在裸机平台上设置应用程序的主函数。

oscPaintedItem.h 和oscPaintedItem.cpp 文件包含 OscillatorPaintedItem 类的定义,该类继承自 PaintedItemDelegate 类。

CMake 项目文件
# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial
cmake_minimum_required (VERSION 3.21.1)

project(painteditem VERSION 0.0.1 LANGUAGES C CXX ASM)
if (NOT TARGET Qul::Core)
    find_package(Qul)
endif()

string(TOLOWER ${QUL_PLATFORM} PLATFORM_LOWERCASE)

set (GENERATE_ENTRYPOINT_ARG "")
if (QUL_OS STREQUAL "FreeRTOS")
    set(GENERATE_ENTRYPOINT_ARG GENERATE_ENTRYPOINT)
endif()

qul_add_target(painteditem QML_PROJECT mcu_painteditem.qmlproject oscPaintedItem.cpp ${GENERATE_ENTRYPOINT_ARG})

if (PLATFORM_LOWERCASE MATCHES "^rh850" OR PLATFORM_LOWERCASE MATCHES "^tvii")
    target_compile_definitions(painteditem PRIVATE NO_TOUCH)
endif()

app_target_setup_os(painteditem)

if (NOT QUL_OS STREQUAL "FreeRTOS")
    target_sources(painteditem PRIVATE
                   main_baremetal.cpp
    )
endif()
QmlProject 文件
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

import QmlProject 1.3

Project {
        mainFile: "oscillator.qml"

        InterfaceFiles {
                files: [
                        "oscPaintedItem.h"
                ]
        }

        ModuleFiles {
                MCU.qulModules: ["Controls"]
        }
}
PaintedItem

PaintedItem Qml 类型的 delegate_ 属性指向 OscillatorPaintedItem 实例。

                    //PaintedItem Object
                    PaintedItem {
                        id: oscillator
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.fill: parent

                        delegate_: OscillatorPaintedItem {
                            id: delegate
                            angle: 0.0
                            initialAngle: 50
                            pivotLength: 50

                            onAngleChanged: {
                                delegate.update()
                            }
                        }
                    }
文本项

初始角度和振荡的固定阻尼系数显示在左上角。

                    Text {
                        anchors.top: parent.top
                        anchors.topMargin: 8
                        anchors.left: parent.left
                        anchors.leftMargin: 8
                        text: "Angle: " + delegate.initialAngle + "\ndamping: " + app.dampingFactor
                        font.pixelSize: 13
                    }
重播按钮

当振荡角度达到 0 时,圆形斑点处于静止状态。按下Replay 按钮后,初始角度将重新设为最大值,动画随之重新开始。

                    Button {
                        id: replay

                        //For platforms not supporting touch input, hide Replay button.
                        visible: !root.automaticAnimation

                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.bottom: parent.verticalCenter
                        height: 40
                        width: 70
                        font.pixelSize: 13
                        text: "Replay"

                        background: Rectangle {
                            color: replay.down ? "#A39D9C" : "#B8B3B2"
                            radius: 8
                        }

                        onClicked: {
                          /*Setting the initialAngle property to maximum value disturbs the equilibrium and
                          triggers the animation again.*/
                          delegate.initialAngle = 50
                        }
动画计时器

该动画基于angle属性,该属性每100毫秒更新一次。每次对angle属性的更新都会触发委托更新,从而调用OscillatorPaintedItem类的paint() 方法。

                    Timer {
                        running: root.automaticAnimation
                        repeat: true
                        interval: 8000
                        onTriggered: delegate.initialAngle = 50
                    }

文件:

在某些 Qt 许可下可用。
了解更多。