1. pro文件中添加配置,然后执行qmake

QT += datavisualization

2. 头文件添加

#include <QtDataVisualization>
#include <QHBoxLayout>
#include <QVBoxLayout>

using namespace QtDataVisualization;

3. cpp代码实现

Q3DSurface *graph = new Q3DSurface();
graph->setFlags(graph->flags() ^ Qt::FramelessWindowHint);
//创建数据对象
QSurfaceDataArray *data = new QSurfaceDataArray;
QSurfaceDataRow *dataRow1 = new QSurfaceDataRow;
QSurfaceDataRow *dataRow2 = new QSurfaceDataRow;
//为数据对象赋值
*dataRow1 << QVector3D(0.0f, 0.1f, 0.5f) << QVector3D(1.0f, 0.5f, 0.5f);
*dataRow2 << QVector3D(0.0f, 1.8f, 1.0f) << QVector3D(1.0f, 1.2f, 1.0f);
//*data << dataRow1 << dataRow2;
data->append(dataRow1);
data->append(dataRow2);
//创建序列,加入数据,添加到Q3DSurface
QSurface3DSeries *series = new QSurface3DSeries;
series->dataProxy()->resetArray(data);
graph->addSeries(series);
graph->setWidth(800);
graph->setHeight(600);
graph->show();

4. 实现效果(鼠标滚轮可以缩放,鼠标右键按住不放可以旋转调整视野角度)

Qt Data Visualization Q3DSurface绘制三维曲面图实例_taro