Qt 数据三维显示


.pro文件

 1 #-------------------------------------------------
 2 #  3 # Project created by QtCreator 2019-01-10T15:27:17
 4 #  5 #-------------------------------------------------
 6 
 7 
 8 QT       += core gui  9 QT       +=datavisualization 10 
11 
12 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 13 
14 
15 TARGET = samp10_1 16 TEMPLATE = app 17 
18 
19 # The following define makes your compiler emit warnings if you use 20 # any feature of Qt which has been marked as deprecated (the exact warnings 21 # depend on your compiler). Please consult the documentation of the 22 # deprecated API in order to know how to port your code away from it. 23 DEFINES += QT_DEPRECATED_WARNINGS 24 
25 
26 # You can also make your code fail to compile if you use deprecated APIs. 27 # In order to do so, uncomment the following line. 28 # You can also select to disable deprecated APIs only up to a certain version of Qt. 29 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
30 
31 
32 CONFIG += c++11
33 
34 
35 SOURCES += \ 36  main.cpp \ 37  mainwindow.cpp 38 
39 
40 HEADERS += \ 41  mainwindow.h 42 
43 
44 FORMS += \ 45  mainwindow.ui 46 
47 
48 # Default rules for deployment. 49 qnx: target.path = /tmp/$${TARGET}/bin 50 else: unix:!android: target.path = /opt/$${TARGET}/bin 51 !isEmpty(target.path): INSTALLS += target

mainwindow.h

 1 #ifndef MAINWINDOW_H  2 #define MAINWINDOW_H
 3 
 4 
 5 #include <QMainWindow>
 6 #include <QSplitter>
 7 #include <QCategory3DAxis>
 8 #include <QtDataVisualization>
 9 using namespace QtDataVisualization;  10 
 11 
 12 namespace Ui {  13 class MainWindow;  14 }  15 
 16 
 17 class MainWindow : public QMainWindow  18 {  19  Q_OBJECT  20 private:  21     QWidget *graphContainer;//图标的容器
 22     Q3DBars *graph3D;//图表
 23     QBar3DSeries *series;//图标的序列
 24     void iniGraph3D();//绘制图表
 25 public:  26     explicit MainWindow(QWidget *parent = nullptr);  27     ~MainWindow();  28     
 29 private slots:  30     void on_comboCamera_currentIndexChanged(int index);  31     
 32     void on_sliderH_valueChanged(int value);  33     
 34     void on_sliderV_valueChanged(int value);  35     
 36     void on_sliderZoom_valueChanged(int value);  37     
 38     void on_cBoxTheme_currentIndexChanged(int index);  39     
 40     void on_cBoxBarStyle_currentIndexChanged(int index);  41     
 42     void on_cBoxSelectionMode_currentIndexChanged(int index);  43     
 44     void on_chkBoxBackground_clicked(bool checked);  45     
 46     void on_chkBoxGrid_clicked(bool checked);  47     
 48     void on_chkBoxAxisBackground_clicked(bool checked);  49     
 50     void on_chkBoxItemLabel_clicked(bool checked);  51     
 52     void on_chkBoxSmooth_clicked(bool checked);  53     
 54     void on_chkBoxReverse_clicked(bool checked);  55     
 56     void on_chkBoxReflection_clicked(bool checked);  57     
 58     void on_chkBoxAxisTitle_clicked(bool checked);  59     
 60     void on_spinFontSize_valueChanged(const QString &arg1);  61     
 62     void on_spinFontSize_valueChanged(int arg1);  63     
 64 private:  65     Ui::MainWindow *ui;  66 };  67 
 68 
 69 #endif // MAINWINDOW_H
 70 #ifndef MAINWINDOW_H  71 #define MAINWINDOW_H
 72 
 73 
 74 #include <QMainWindow>
 75 #include <QSplitter>
 76 #include <QCategory3DAxis>
 77 #include <QtDataVisualization>
 78 using namespace QtDataVisualization;  79 
 80 
 81 namespace Ui {  82 class MainWindow;  83 }  84 
 85 
 86 class MainWindow : public QMainWindow  87 {  88  Q_OBJECT  89 private:  90     QWidget *graphContainer;//图标的容器
 91     Q3DBars *graph3D;//图表
 92     QBar3DSeries *series;//图标的序列
 93     void iniGraph3D();//绘制图表
 94 public:  95     explicit MainWindow(QWidget *parent = nullptr);  96     ~MainWindow();  97     
 98 private slots:  99     void on_comboCamera_currentIndexChanged(int index); 100     
101     void on_sliderH_valueChanged(int value); 102     
103     void on_sliderV_valueChanged(int value); 104     
105     void on_sliderZoom_valueChanged(int value); 106     
107     void on_cBoxTheme_currentIndexChanged(int index); 108     
109     void on_cBoxBarStyle_currentIndexChanged(int index); 110     
111     void on_cBoxSelectionMode_currentIndexChanged(int index); 112     
113     void on_chkBoxBackground_clicked(bool checked); 114     
115     void on_chkBoxGrid_clicked(bool checked); 116     
117     void on_chkBoxAxisBackground_clicked(bool checked); 118     
119     void on_chkBoxItemLabel_clicked(bool checked); 120     
121     void on_chkBoxSmooth_clicked(bool checked); 122     
123     void on_chkBoxReverse_clicked(bool checked); 124     
125     void on_chkBoxReflection_clicked(bool checked); 126     
127     void on_chkBoxAxisTitle_clicked(bool checked); 128     
129     void on_spinFontSize_valueChanged(const QString &arg1); 130     
131     void on_spinFontSize_valueChanged(int arg1); 132     
133 private: 134     Ui::MainWindow *ui; 135 }; 136 
137 
138 #endif // MAINWINDOW_H

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 
 5 MainWindow::MainWindow(QWidget *parent) :  6 QMainWindow(parent),  7 ui(new Ui::MainWindow)  8 {  9 ui->setupUi(this);  10 iniGraph3D();  11 QSplitter *splitter =new QSplitter(Qt::Horizontal);  12 splitter->addWidget(ui->groupBox);  13 splitter->addWidget(graphContainer);  14 this->setCentralWidget(splitter);  15 
 16 
 17 }  18 MainWindow::~MainWindow()  19 {  20 delete ui;  21 }  22 void MainWindow::iniGraph3D()  23 {  24     //创建图表
 25     graph3D =new Q3DBars();  26     graphContainer=QWidget::createWindowContainer(graph3D);  27     //创建坐标轴
 28  QStringList rowLabs;  29  QStringList colLabs;  30     rowLabs<<"row1"<<"row2"<<"row3";  31     colLabs<<"col1"<<"col2"<<"col3"<<"col4"<<"col5";  32     QValue3DAxis *axisV=new QValue3DAxis;  33     axisV->setTitle("value");  34     axisV->setTitleVisible(true);  35     QCategory3DAxis *axisRow=new QCategory3DAxis;//行坐标轴
 36     axisRow->setTitle("row axis");  37     axisRow->setLabels(rowLabs);  38     axisRow->setTitleVisible(true);  39     QCategory3DAxis *axisCol=new QCategory3DAxis;//列坐标轴
 40     axisCol->setTitle("col axis");  41     axisCol->setLabels(colLabs);  42     axisCol->setTitleVisible(true);  43     //将坐标轴添加到图像
 44     graph3D->setRowAxis(axisRow);  45     graph3D->setValueAxis(axisV);  46     graph3D->setColumnAxis(axisCol);  47     //创建序列
 48     series =new QBar3DSeries;  49     series->setMesh(QAbstract3DSeries::MeshCylinder);//棒状形状
 50     series->setItemLabelFormat("(@rowLabel,@colLabel):%.1f");//标签格式
 51     graph3D->addSeries(series);  52     //添加数据
 53     QBarDataArray *dataSet=new QBarDataArray;//数据数组
 54     dataSet->reserve(rowLabs.count());  55     QBarDataRow *dataRow=new QBarDataRow;  56     *dataRow<<1<<2<<3<<4<<5;  57     dataSet->append(dataRow);  58     QBarDataRow *dataRow2=new QBarDataRow;  59     *dataRow2<<5<<5<<5<<5<<5;  60     dataSet->append(dataRow2);  61     QBarDataRow *dataRow3=new QBarDataRow;  62     *dataRow3<<1<<5<<9<<5<<1;  63     dataSet->append(dataRow3);  64 
 65 
 66     //为序列的数据代理设置数据
 67     series->dataProxy()->resetArray(dataSet);  68 }  69 
 70 
 71 /**  72  * @brief MainWindow::on_comboCamera_currentIndexChanged  73  * @param index  74  */
 75 void MainWindow::on_comboCamera_currentIndexChanged(int index)  76 {  77     //变换视角
 78     Q3DCamera::CameraPreset cameraPos=Q3DCamera::CameraPreset(index);//将序号转换成Q3DCamera::CameraPreset类型
 79     graph3D->scene()->activeCamera()//获取当前图标视角
 80         ->setCameraPreset(cameraPos);//设置视角
 81 }  82 
 83 
 84 void MainWindow::on_sliderH_valueChanged(int value)  85 {  86  Q_UNUSED(value);  87     int xRot=ui->sliderH->value();  88     //int yRot=ui->sliderV->value();  89     //int zoom=ui->sliderZoom->value();
 90     graph3D->scene()->activeCamera()->setXRotation(xRot);  91     //graph3D->scene()->activeCamera()->setYRotation(yRot);  92     //graph3D->scene()->activeCamera()->setZoomLevel(zoom);
 93 }  94 
 95 
 96 void MainWindow::on_sliderV_valueChanged(int value)  97 {  98  Q_UNUSED(value);  99     int yRot=ui->sliderV->value(); 100     graph3D->scene()->activeCamera()->setYRotation(yRot); 101 } 102 
103 
104 void MainWindow::on_sliderZoom_valueChanged(int value) 105 { 106  Q_UNUSED(value); 107     int zoom=ui->sliderZoom->value(); 108     graph3D->scene()->activeCamera()->setZoomLevel(zoom); 109 } 110 //修改主题
111 void MainWindow::on_cBoxTheme_currentIndexChanged(int index) 112 { 113     Q3DTheme *currentTheme=graph3D->activeTheme(); 114     currentTheme->setType(Q3DTheme::Theme(index)); 115 } 116 //修改棒图样式
117 void MainWindow::on_cBoxBarStyle_currentIndexChanged(int index) 118 { 119  QAbstract3DSeries::Mesh aMesh; 120     aMesh=QAbstract3DSeries::Mesh(index+1); 121     series->setMesh(aMesh); 122 } 123 //修改选择模式
124 void MainWindow::on_cBoxSelectionMode_currentIndexChanged(int index) 125 { 126     graph3D->setSelectionMode(QAbstract3DGraph::SelectionFlags(index)); 127 } 128 //背景可见
129 void MainWindow::on_chkBoxBackground_clicked(bool checked) 130 { 131     graph3D->activeTheme()->setBackgroundEnabled(checked); 132 } 133 //背景网络
134 void MainWindow::on_chkBoxGrid_clicked(bool checked) 135 { 136     graph3D->activeTheme()->setGridEnabled(checked); 137 
138 
139 } 140 
141 
142 //坐标轴标签背景可见
143 void MainWindow::on_chkBoxAxisBackground_clicked(bool checked) 144 { 145     graph3D->activeTheme()->setLabelBackgroundEnabled(checked); 146 } 147 //项的标签可见
148 void MainWindow::on_chkBoxItemLabel_clicked(bool checked) 149 { 150     series->setItemLabelFormat("value at (@rowLabel,@colLabel):%.1f"); 151     series->setItemLabelVisible(checked); 152 } 153 //光滑
154 void MainWindow::on_chkBoxSmooth_clicked(bool checked) 155 { 156     series->setMeshSmooth(checked); 157 } 158 //数值轴反转
159 void MainWindow::on_chkBoxReverse_clicked(bool checked) 160 { 161     graph3D->valueAxis()->setReversed(checked); 162 } 163 //设置倒影
164 void MainWindow::on_chkBoxReflection_clicked(bool checked) 165 { 166     graph3D->setReflection(checked); 167 } 168 //标签可见
169 void MainWindow::on_chkBoxAxisTitle_clicked(bool checked) 170 { 171     graph3D->rowAxis()->setTitleVisible(checked); 172     graph3D->columnAxis()->setTitleVisible(checked); 173     graph3D->valueAxis()->setTitleVisible(checked); 174 } 175 
176 
177 void MainWindow::on_spinFontSize_valueChanged(const QString &arg1) 178 { 179 
180 
181 } 182 //设置字体大小
183 void MainWindow::on_spinFontSize_valueChanged(int arg1) 184 { 185     QFont font=graph3D->activeTheme()->font(); 186  font.setPointSize(arg1); 187     graph3D->activeTheme()->setFont(font); 188 }

运行结果
在这里插入图片描述


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM