讲解都在注释中。
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;
}