QT的Q3DScatter使用(三维可视图)

讲解都在注释中。

3d.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2018-03-12T18:11:08
#
#-------------------------------------------------

QT       += core gui datavisualization widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = 3d
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui
target.path = $$[QT_INSTALL_EXAMPLES]/datavisualization/$$TARGET
INSTALLS += target

main.cpp

#include "mainwindow.h"
#include <QApplication>
using namespace QtDataVisualization;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindows.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QVBoxLayout>
#include "scatterdatamodifier.h"
#include <QtWidgets/QMessageBox>
using namespace QtDataVisualization;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //创建一个三维坐标系
    Q3DScatter *graph = new Q3DScatter();
    //设置相机的位置
    graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetBehindLow);
    //设置阴影显示
    graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);
    //设置坐标轴的范围
    graph->axisX()->setRange(0, 480);
    graph->axisY()->setRange(0, 480);
    graph->axisZ()->setRange(0, 480);
    //设置坐标轴的数量
    graph->axisX()->setSegmentCount(int(480/20));
    graph->axisY()->setSegmentCount(int(480/20));
    graph->axisZ()->setSegmentCount(int(480/20));
    //设置整体坐标系的主题
    graph->activeTheme()->setType(Q3DTheme::ThemeQt);

    graph->axisX()->setLabelFormat("X");
    graph->axisY()->setLabelFormat("Y");
    graph->axisZ()->setLabelFormat("Z");

    //点
    QScatter3DSeries *series = new QScatter3DSeries;
    //点,大小
    series->setItemSize(0.05);
    //点,坐标
    QScatterDataArray data;

//    data << QVector3D(0.5f, 0.5f, 0.5f);
    //创建一个widget,将坐标系添加进去
    QWidget *container = QWidget::createWindowContainer(graph);

    //判断是否graph(opengl)初始化
    if (!graph->hasContext()) {
        QMessageBox msgBox;
        msgBox.setText("Couldn't initialize the OpenGL context.");
        msgBox.exec();
    }
    //水平布局,父对象是wifget
    QHBoxLayout *hLayout = new QHBoxLayout(ui->widget);
    //垂直布局
    QVBoxLayout *vLayout = new QVBoxLayout();
    //将container添加到水平布局中
    hLayout->addWidget(container, 1);
    hLayout->addLayout(vLayout);
    data.append(QVector3D(0.5f, 0.5f, 0.5f));
    data.append(QVector3D(10, 10, 10));
    series->dataProxy()->addItems(data);
    graph->addSeries(series);
}

MainWindow::~MainWindow()
{
    delete ui;
}


  • 6
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
使用Qt来开发三维设计软件是一种常见的选择。Qt是一款跨平台的C++应用开发框架,具有丰富图形渲染和用户界面功能,常适合用于开发三维设计件。下面是使用Qt开发三维设计软件的一般步骤: 1. 项目设置:创建一个Qt项目,并设置适当的项目属性和依赖库。确保项目包含所需的三维渲染和图形库,如Qt 3D等。 2. 界面设计:使用Qt的UI设计器创建用户友好的界面,包括工具栏、视图窗口、属性面板等元素。用户可以通过界面进行三维模型的创建、编辑和查看。 3. 三维模型创建:使用Qt 3D提供的功能创建三维模型。Qt 3D提供了一套强大的三维图形渲染和场景管理工具,可以使用C++或QML编写代码来创建和操作三维模型。 4. 模型编辑:实现对三维模型的编辑功能,如平移、旋转、缩放等操作。通过Qt的输入事件和变换操作,可以实现对模型的交互式编辑。 5. 材质和纹理:为三维模型应用适当的材质和纹理。Qt 3D提供了各种材质类型和渲染效果,可以根据需求进行设置。 6. 光照和阴影:添加光源来照亮三维模型,并生成逼真的阴影效果。Qt 3D支持不同类型的光源,如定向光、点光源、聚光灯等。 7. 动画和交互:实现模型的动画效果和用户交互。Qt提供了动画框架和交互手段,可以创建模型的变换动画或响应用户操作的交互效果。 8. 导出和保存:实现将三维模型导出为文件或保存到数据库的功能。这样用户可以在其他应用程序中使用或分享他们的设计。 需要注意的是,使用Qt开发三维设计软件需要熟悉C++编程和Qt框架的知识。同时,为了保证软件在不同操作系统上的兼容性和性能表现,需要进行适当的优化和测试。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值