? ? ? ? 有时候我们的数据要用三维坐标显示。最近做了个小案例,分享一下源码。
?备注:滚轮可缩放,右键可旋转。
目录
配置安装
源码
工程文件包
配置安装
? ? ? ? Qt的三维坐标要用到Data?Visualization模块,需要用Qt?Maintenance?Tool安装这个模块。
源码
pro文件
QT += core gui datavisualization
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp文件不变就不展示了。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtDataVisualization>
using namespace QtDataVisualization;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_comboCamera_currentIndexChanged(int index);
void on_sliderH_valueChanged(int value);
void on_sliderV_valueChanged(int value);
void on_sliderZoom_valueChanged(int value);
void on_comboTheme_currentIndexChanged(int index);
void on_checkBoxBackground_clicked(bool checked);
void on_checkBoxGrid_clicked(bool checked);
void on_checkBoxAxisBackground_clicked(bool checked);
void on_pushButtonAddData_clicked();
private:
QWidget *graphContainer; //图表的容器
Q3DScatter *graph3D; //散点图
QScatter3DSeries *series; //散点序列
void initGraph3D(); //初始化
public:
//为实现数据共享,公共化参数
QScatterDataArray *dataArray;
QScatterDataItem *ptrToDataArray;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSplitter>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initGraph3D(); //初始化散点图
ui->verticalLayout_graph3D->addWidget(graphContainer);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initGraph3D()
{
//创建图表
graph3D = new Q3DScatter();
graphContainer = QWidget::createWindowContainer(graph3D);
QScatterDataProxy *proxy = new QScatterDataProxy(); //数据代理
series = new QScatter3DSeries(proxy); //创建序列
series->setItemLabelFormat("@xLabel @yLabel @zLabel");
series->setMeshSmooth(true);
graph3D->addSeries(series);
//创建坐标轴
graph3D->axisX()->setTitle("axis X");
graph3D->axisX()->setTitleVisible(true);
graph3D->axisY()->setTitle("axis Y");
graph3D->axisY()->setTitleVisible(true);
graph3D->axisZ()->setTitle("axis Z");
graph3D->axisZ()->setTitleVisible(true);
graph3D->activeTheme()->setLabelBackgroundEnabled(false);
series->setMesh(QAbstract3DSeries::MeshSphere); //数据点为圆球
series->setItemSize(0.2); //取值范围0~1 ,自动放缩因子
int N = 41;
int itemCount = N*N;
dataArray = new QScatterDataArray();
dataArray->resize(itemCount); //41*41个点
ptrToDataArray = &dataArray->first();
//墨西哥草帽, -10:0.5:10, N=41
float x,y,z;
int i,j;
x=-10;
for(i=1;i<N;i++)
{
y=10;
for(j=1;j<=N;j++)
{
z=qSqrt(x*x+y*y);
if(z!=0)
{
z=10*qSin(z)/z;
}
else
{
z=10;
}
ptrToDataArray->setPosition(QVector3D(x,y,z)); //添加点数据
ptrToDataArray++;//指针加一
y+=0.5;
}
x+=0.5;
}
series->dataProxy()->resetArray(dataArray);
}
void MainWindow::on_comboCamera_currentIndexChanged(int index)
{
Q3DCamera::CameraPreset cameraPos = Q3DCamera::CameraPreset(index);
graph3D->scene()->activeCamera()->setCameraPreset(cameraPos);
}
void MainWindow::on_sliderH_valueChanged(int value)
{
Q_UNUSED(value);
int xRot = ui->sliderH->value(); //水平
int yRot = ui->sliderV->value(); //垂直
int zoom = ui->sliderZoom->value(); //缩放
graph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zoom);
}
void MainWindow::on_sliderV_valueChanged(int value)
{
Q_UNUSED(value);
int xRot = ui->sliderH->value(); //水平
int yRot = ui->sliderV->value(); //垂直
int zoom = ui->sliderZoom->value(); //缩放
graph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zoom);
}
void MainWindow::on_sliderZoom_valueChanged(int value)
{
Q_UNUSED(value);
int xRot = ui->sliderH->value(); //水平
int yRot = ui->sliderV->value(); //垂直
int zoom = ui->sliderZoom->value(); //缩放
graph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zoom);
}
void MainWindow::on_comboTheme_currentIndexChanged(int index)
{
//设置主题
Q3DTheme *currentTheme = graph3D->activeTheme();
currentTheme->setType(Q3DTheme::Theme(index)); //可以打开帮助看有什么主题
}
void MainWindow::on_checkBoxBackground_clicked(bool checked)
{
//图表的背景
graph3D->activeTheme()->setBackgroundEnabled(checked);
}
void MainWindow::on_checkBoxGrid_clicked(bool checked)
{
//图标的网格
graph3D->activeTheme()->setGridEnabled(checked);
}
void MainWindow::on_checkBoxAxisBackground_clicked(bool checked)
{
//轴标签背景
graph3D->activeTheme()->setLabelBackgroundEnabled(checked);
}
void MainWindow::on_pushButtonAddData_clicked()
{
int N = 42;
int itemCount = N*N;
// dataArray = new QScatterDataArray();
dataArray->resize(itemCount); //41*41个点
// ptrToDataArray = &dataArray->first();
int x=0;
int y=0;
int z=0;
//产生0~9的随机数
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(int i=0; i<10; i++)
{
x =qrand()%10;
y =qrand()%10;
z =qrand()%10;
}
ptrToDataArray->setPosition(QVector3D(x,y,z)); //添加点数据
ptrToDataArray++;//指针加一
series->dataProxy()->resetArray(dataArray); //更新数据
}
工程文件包
3DScatterChart.zip-QT文档类资源-CSDN下载Qt3D散点图更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/laoxue123456/38621029
|