讲解都在注释中。
3d.pro文件
1 #-------------------------------------------------
2 # 3 # Project created by QtCreator 2018-03-12T18:11:08
4 # 5 #-------------------------------------------------
6
7 QT += core gui datavisualization widgets 8
9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10
11 TARGET = 3d 12 TEMPLATE = app 13
14 # The following define makes your compiler emit warnings if you use 15 # any feature of Qt which has been marked as deprecated (the exact warnings 16 # depend on your compiler). Please consult the documentation of the 17 # deprecated API in order to know how to port your code away from it. 18 DEFINES += QT_DEPRECATED_WARNINGS 19
20 # You can also make your code fail to compile if you use deprecated APIs. 21 # In order to do so, uncomment the following line. 22 # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 SOURCES += \ 25 main.cpp \ 26 mainwindow.cpp 27
28 HEADERS += \ 29 mainwindow.h 30
31 FORMS += \ 32 mainwindow.ui 33 target.path = $$[QT_INSTALL_EXAMPLES]/datavisualization/$$TARGET 34 INSTALLS += target
main.cpp
1 #include "mainwindow.h"
2 #include <QApplication>
3 using namespace QtDataVisualization; 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 MainWindow w; 8 w.show(); 9 return a.exec(); 10 }
mainwindows.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <QtWidgets/QHBoxLayout>
4 #include <QtWidgets/QVBoxLayout>
5 #include "scatterdatamodifier.h"
6 #include <QtWidgets/QMessageBox>
7 using namespace QtDataVisualization; 8
9 MainWindow::MainWindow(QWidget *parent) : 10 QMainWindow(parent), 11 ui(new Ui::MainWindow) 12 { 13 ui->setupUi(this); 14
15 //创建一个三维坐标系
16 Q3DScatter *graph = new Q3DScatter(); 17 //设置相机的位置
18 graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetBehindLow); 19 //设置阴影显示
20 graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone); 21 //设置坐标轴的范围
22 graph->axisX()->setRange(0, 480); 23 graph->axisY()->setRange(0, 480); 24 graph->axisZ()->setRange(0, 480); 25 //设置坐标轴的数量
26 graph->axisX()->setSegmentCount(int(480/20)); 27 graph->axisY()->setSegmentCount(int(480/20)); 28 graph->axisZ()->setSegmentCount(int(480/20)); 29 //设置整体坐标系的主题
30 graph->activeTheme()->setType(Q3DTheme::ThemeQt); 31
32 graph->axisX()->setLabelFormat("X"); 33 graph->axisY()->setLabelFormat("Y"); 34 graph->axisZ()->setLabelFormat("Z"); 35
36 //点
37 QScatter3DSeries *series = new QScatter3DSeries; 38 //点,大小
39 series->setItemSize(0.05); 40 //点,坐标
41 QScatterDataArray data; 42
43 // data << QVector3D(0.5f, 0.5f, 0.5f); 44 //创建一个widget,将坐标系添加进去
45 QWidget *container = QWidget::createWindowContainer(graph); 46
47 //判断是否graph(opengl)初始化
48 if (!graph->hasContext()) { 49 QMessageBox msgBox; 50 msgBox.setText("Couldn't initialize the OpenGL context."); 51 msgBox.exec(); 52 } 53 //水平布局,父对象是wifget
54 QHBoxLayout *hLayout = new QHBoxLayout(ui->widget); 55 //垂直布局
56 QVBoxLayout *vLayout = new QVBoxLayout(); 57 //将container添加到水平布局中
58 hLayout->addWidget(container, 1); 59 hLayout->addLayout(vLayout); 60 data.append(QVector3D(0.5f, 0.5f, 0.5f)); 61 data.append(QVector3D(10, 10, 10)); 62 series->dataProxy()->addItems(data); 63 graph->addSeries(series); 64 } 65
66 MainWindow::~MainWindow() 67 { 68 delete ui; 69 } 70