QML Hello World 入门示例

本文实现一个带动画效果的 Hello World:程序启动后,"Hello World" 文字从窗口顶部弹跳到中间,同时颜色从白色渐变到深灰色,整个动画持续 2 秒。

通过这个 demo,读者可以掌握 Qt Quick 中 NumberAnimation 和 ColorAnimation 两种基础动画组件的使用方法,以及 Component.onCompleted 信号的触发时机。

完整可复用代码

在 Qt Creator 中创建 Qt Quick Application 项目,将 main.qml 文件内容替换为以下代码:

qml 复制代码
import QtQuick
import QtQuick.Window

Window {
    id: root
    width: 400
    height: 300
    visible: true
    color: "#fff"
    title: "QML ------ Hello World"

    Text {
        id: helloText
        text: "Hello World"
        color: "#fff"
        font.pixelSize: 48
        font.bold: true
        anchors.horizontalCenter: parent.horizontalCenter
        y: 0

        ColorAnimation {
            id: colorAnim
            target: helloText
            property: "color"
            from: "#fff"
            to: "#333"
            duration: 2000
        }

        NumberAnimation {
            id: bounceAnim
            target: helloText
            property: "y"
            from: 0
            to: (root.height - helloText.height) / 2
            duration: 2000
        }

        Component.onCompleted: {
            bounceAnim.start()
            colorAnim.start()
        }
    }
}

关键逻辑解析

这段代码的核心逻辑分为三个部分:

1. 窗口与文本基础设置

qml 复制代码
Window {
    id: root
    width: 400
    height: 300
    visible: true
    color: "#fff"
    title: "QML ------ Hello World"
}

Window 是 QML 的顶层窗口组件,visible: true 让窗口显示,color: "#fff" 设置白色背景。

qml 复制代码
Text {
    id: helloText
    text: "Hello World"
    color: "#fff"
    font.pixelSize: 48
    font.bold: true
    anchors.horizontalCenter: parent.horizontalCenter
    y: 0
}

文本初始颜色设为白色(与背景相同),所以启动时文字是"看不见"的。y: 0 让文字位于窗口顶部。

2. 两种动画效果

颜色动画:让文字从白色渐变到深灰色

qml 复制代码
ColorAnimation {
    id: colorAnim
    target: helloText
    property: "color"
    from: "#fff"
    to: "#333"
    duration: 2000
}

位移动画:让文字从顶部移动到垂直居中位置

qml 复制代码
NumberAnimation {
    id: bounceAnim
    target: helloText
    property: "y"
    from: 0
    to: (root.height - helloText.height) / 2
    duration: 2000
}

两个动画的 duration 都是 2000 毫秒,同步执行产生组合效果。

3. 组件加载完成后启动动画

qml 复制代码
Component.onCompleted: {
    bounceAnim.start()
    colorAnim.start()
}

Component.onCompleted 在组件加载完成后自动触发,在这里同时启动两个动画。

运行验证

打开 Qt Creator,点击打开项目,找到 qml_hello 示例的 CMakeLists.txt 文件;

点击左下角绿色三角形"运行"按钮,或按 Ctrl+R

观察窗口:文字从顶部弹跳到中间,同时颜色从白色渐变到深灰色

扩展复用方向

读者可以直接复用这段动画逻辑,或基于此扩展更丰富的效果:

  • 添加交互 :在 Text 外层包裹 MouseArea,点击时重新播放动画
  • 替换动画类型 :将 NumberAnimation 替换为 RotationAnimation,实现旋转效果
  • 组合多个动画 :使用 ParallelAnimation 或 SequentialAnimation 组合更多动画
  • 结合状态机 :使用 State 和 Transition 实现更复杂的交互状态切换
qml 复制代码
// 点击重新播放动画的示例
MouseArea {
    anchors.fill: parent
    onClicked: {
        bounceAnim.restart()
        colorAnim.restart()
    }
}

已验证环境:

相关推荐
慧都小项3 天前
当边缘AI上产线:QtitanDocking 如何让工业 HMI 实现多视图协同
人工智能·qt·ui·边缘计算·qt6.3
Quz3 天前
QML SwipeDelegate 滑动委托
qt
Quz3 天前
QML 列表中的四种委托:ItemDelegate、CheckDelegate、RadioDelegate、SwitchDelegate
qt
实心儿儿3 天前
Qt — 显示类控件
qt
江湖人称菠萝包4 天前
【Qt】《Qt 5.9 C++开发指南》笔记-Chapter9-Qt Charts
笔记·qt·qt5
扶尔魔ocy4 天前
【QT android】环境安装及第一个QT项目
qt·andriod开发
江湖人称菠萝包4 天前
【Qt】《Qt 5.9 C++开发指南》笔记-Chapter10-Data Visualization
笔记·qt·qt5
Cx330❀4 天前
Qt 常用控件属性与底层机制详解:从窗口半透明、浮点数陷阱到 QSS 与焦点策略
qt·ui·图形渲染
Quz4 天前
QML DelegateChooser:多条件组合与按列选择委托
qt
Quz4 天前
QML DelegateChooser:按角色选择委托与按索引选择委托
qt