Qt Quick 示例 - 项目可变刷新率

Qt Quick 示例展示了如何为用户界面的特定部分设置可变刷新率。

该示例演示了如何使用Item Layers 的实时属性为快速项目获取可变且独立的刷新率。如果用户界面的特定部分不需要每帧重新渲染,但仍需要内部更新,这将大大提高性能。绘制大部分时间都是静态的部分或不需要一直流畅运行的动画会浪费资源。该属性可让用户控制何时绘制项目。

注意: 使用Item Layers 可能会影响性能,这取决于分层项子树的大小,因为要使用单独的渲染目标(纹理),然后在主渲染通道中将它们混合在一起。应用程序设计人员应始终评估其影响,以确保使用分层的好处大于潜在的坏处。

本示例使用FrameAnimation 来控制项目的渲染频率。当你只想每隔一帧渲染一次时,这种方法非常有用。使用计时器或其他机械装置来切换标志也是有效的。

FrameAnimation {
  id: frameAnimation
  property real fps: smoothFrameTime > 0 ? (1.0 / smoothFrameTime) : 0
  running: true
}

在上述代码段中,fps 属性仅用于视觉演示目的。

该项目要求 layer.enabled 为 true,然后该项目将根据 layer.live 的状态进行渲染,如下所示。

Item {
    id: qt_logo
    width: 230
    height: 230
    anchors.fill: parent
    anchors.topMargin: 125
    layer.enabled: true
    layer.live: slider.value > 0 && frameAnimation.currentFrame % slider.value == 0

通过使用调制运算符和滑块设置值,我们可以动态改变 Qt 徽标的呈现频率。

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.