Qt Quick 3D - XR 简单示例

演示如何使用Qt Quick 3D XR 搭建一个简单的 VR 场景。

包含“Qt 6 in VR”文字及三个彩色立方体的VR场景

本示例简要介绍了Qt Quick 3D XR的基本功能。

编写 XR 应用程序时的主要区别在于,main.qml 文件的根项是XrView ,而不是带有View3D 的 Window:

import QtQuick3D
import QtQuick3D.Xr

XrView {
    id: xrView
    XrErrorDialog { id: err }
    onInitializeFailed: (errorString) => err.run("XRView", errorString)
    referenceSpace: XrView.ReferenceSpaceLocalFloor

XrView 包含3D场景,这与View3D 类似。在本示例中,我们添加了一个XrErrorDialog ,当从台式电脑流式传输时,若初始化VR头显出现错误,该组件将显示错误对话框。我们还指定了该应用程序的参考帧以用户所在房间的地板为基准。也就是说,地板位于y = 0 。

参考坐标系的原点由XrOrigin 定义:

    xrOrigin: theOrigin
    XrOrigin {
        id: theOrigin

        XrController {
            controller: XrController.LeftController
            poseSpace: XrController.AimPose
            CubeModel { color: "blue" }
        }

        XrController {
            controller: XrController.RightController
            poseSpace: XrController.AimPose
            CubeModel { color: "red" }
        }
    }

在此示例中,我们添加了两个XrController 项,每只手各一个。它们将跟踪用户双手的位姿。由于XrController 是一个Node ,因此其任何子元素都会自动跟随手部动作。在此情况下,我们使用一个简单的立方体模型来可视化控制器的位置。

main.qml 的其余部分是一个标准的Qt Quick 3D 场景。

示例项目 @ code.qt.io

© 2026 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.