You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为QML中的Scatter3D图表添加旋转功能?

为Qt Scatter3D添加旋转功能的实现方法

一、启用默认鼠标交互旋转

Qt Data Visualization的Scatter3D默认支持鼠标拖拽旋转视图,只需确保interactive属性处于开启状态(默认值为true,若被手动关闭需重新设置):

Scatter3D {
    id: scatter3d
    width: parent.width
    height: parent.height
    anchors.centerIn: parent
    // 确保交互功能开启,默认已开启,若曾关闭需恢复
    interactive: true

    theme: Theme3D {
        backgroundColor: "black"
    }

    Scatter3DSeries {
        baseColor: "red"
        ItemModelScatterDataProxy {
            itemModel: dataModel
            xPosRole: "xPos"
            yPosRole: "yPos"
            zPosRole: "zPos"
        }
    }
}

设置完成后,按住鼠标左键拖拽图表即可自由旋转视角。

二、代码控制旋转(自动/按钮触发)

如果需要通过代码主动控制旋转逻辑(比如自动旋转、按钮启停),可以操作Scatter3D的camera属性,调整相机的xRotation和yRotation参数:

1. 自动旋转示例

添加定时器持续修改相机角度,实现自动旋转效果:

Scatter3D {
    id: scatter3d
    width: parent.width
    height: parent.height
    anchors.centerIn: parent

    theme: Theme3D {
        backgroundColor: "black"
    }

    Scatter3DSeries {
        baseColor: "red"
        ItemModelScatterDataProxy {
            itemModel: dataModel
            xPosRole: "xPos"
            yPosRole: "yPos"
            zPosRole: "zPos"
        }
    }

    // 自动旋转定时器
    Timer {
        id: rotateTimer
        interval: 30  // 间隔越小旋转速度越快
        running: true
        repeat: true
        onTriggered: {
            // 沿Y轴循环旋转,每次增加1度
            scatter3d.camera.yRotation = (scatter3d.camera.yRotation + 1) % 360;
            // 如需同时沿X轴旋转,取消下方注释
            // scatter3d.camera.xRotation = (scatter3d.camera.xRotation + 0.5) % 360;
        }
    }
}

2. 按钮控制旋转启停与重置

添加按钮组件,手动控制旋转的开启、停止及视角重置:

// 按钮控制栏
Row {
    anchors.bottom: parent.bottom
    anchors.horizontalCenter: parent.horizontalCenter
    spacing: 10

    Button {
        text: "开始旋转"
        onClicked: rotateTimer.running = true
    }

    Button {
        text: "停止旋转"
        onClicked: rotateTimer.running = false
    }

    Button {
        text: "重置视角"
        onClicked: {
            scatter3d.camera.xRotation = 30; // 还原默认X轴角度
            scatter3d.camera.yRotation = 0;  // 还原默认Y轴角度
        }
    }
}

Scatter3D {
    id: scatter3d
    width: parent.width
    height: parent.height
    anchors.centerIn: parent

    theme: Theme3D {
        backgroundColor: "black"
    }

    Scatter3DSeries {
        baseColor: "red"
        ItemModelScatterDataProxy {
            itemModel: dataModel
            xPosRole: "xPos"
            yPosRole: "yPos"
            zPosRole: "zPos"
        }
    }
}

// 旋转控制定时器
Timer {
    id: rotateTimer
    interval: 30
    running: false
    repeat: true
    onTriggered: {
        scatter3d.camera.yRotation = (scatter3d.camera.yRotation + 1) % 360;
    }
}

三、自定义旋转范围与速度

可以通过修改定时器interval值调整旋转速度,也可以限制相机旋转的角度范围,避免视角异常:

Timer {
    id: rotateTimer
    interval: 20
    running: true
    repeat: true
    onTriggered: {
        let newYRot = (scatter3d.camera.yRotation + 1.5) % 360;
        let newXRot = scatter3d.camera.xRotation + 0.8;
        // 限制X轴旋转在0-60度之间,防止视角倒置
        if (newXRot > 60) newXRot = 60;
        if (newXRot < 0) newXRot = 0;
        scatter3d.camera.xRotation = newXRot;
        scatter3d.camera.yRotation = newYRot;
    }
}

内容的提问来源于stack exchange,提问作者Jeyanth Inba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:22:13