本页内容

Qt Graphs 从 Qt DataVisualization 迁移

Qt DataVisualization与Qt Graphs 之间的API和功能基本保持不变。但在将应用程序从Qt DataVisualization迁移到Qt Graphs 时,仍需注意以下几点差异:

  • QML导入语句
  • CMake模块包含
  • qmake模块包含
  • 小部件应用程序的创建
  • 使用 OpenGL 后端的要求
  • 已移除的类
  • 已移除的 API
  • 已更改的 API

QML 导入语句

Qt DataVisualization 中的导入语句:

import QtDataVisualization

已更改为:

import QtGraphs

Qt Graphs 。

CMake 模块包含

Qt DataVisualization 中的包含语句:

find_package(Qt6 REQUIRED COMPONENTS DataVisualization)
target_link_libraries(mytarget PRIVATE Qt6::DataVisualization)

已更改为:

find_package(Qt6 REQUIRED COMPONENTS Graphs)
target_link_libraries(mytarget PRIVATE Qt6::Graphs)

Qt Graphs 。

Qmake 模块引入

在 Qt DataVisualization 中的引入:

QT += datavisualization

已更改为:

QT += graphs

针对Qt Graphs 。

小部件应用程序的创建

要在控件应用程序中创建图表,请将代码从:

Q3DBars *barGraph = new Q3DBars();
QWidget *barsWidget = new QWidget();
QWidget *container = QWidget::createWindowContainer(barGraph, barsWidget);

auto *hLayout = new QHBoxLayout(barsWidget);
hLayout->addWidget(container, 1);

修改为:

QQuickWidget *quickWidget = new QQuickWidget();
Q3DBarsWidgetItem *barGraph = new Q3DBarsWidgetItem();
barGraph->setWidget(quickWidget);

auto *hLayout = new QHBoxLayout(quickWidget);

使用 OpenGL 后端的要求

要使用Qt Graphs 3D,您不再需要强制使用 OpenGL 后端:

// Remove this line
qputenv("QSG_RHI_BACKEND", "opengl");

Qt Graphs 该库使用Qt Quick 3D 进行渲染,因此支持运行平台原生的渲染后端。

已移除的类

  • Q3DCamera
  • Q3DLight
  • Q3DObject
  • Q3DTheme

已移除的 API

  • hasContext
  • shadowsSupported
  • reflection
  • reflectivity

已更改的 API

  • 优化提示
  • 渲染模式
  • 渲染为图像
  • ColorGradient 和 ColorGradientStop
  • 主题颜色
  • 数据 API
  • 相机 API
  • 主题 API
  • 枚举

optimizationHint

Legacy 现在是 QtDataVisualization 中名为 `OptimizationDefault ` 的模式。Default 使用实例化技术,应应用于所有支持该功能的目标。

renderingMode

RenderDirectToBackground_NoClear 已被移除,因为它在 Qt 6 的 QtDataVisualization 中已过时。

renderToImage

renderToImage 现在返回QSharedPointer<QQuickItemGrabResult> 而非QImage ,并且不再接受msaaSamples 作为参数。

ColorGradient和ColorGradientStop

ColorGradient 现已分别更名为Gradient 和ColorGradientStop GradientStop 。

ThemeColor

ThemeColor 现已更名为Color 。

数据 API

不再需要使用 `new ` 来创建数据数组。例如,在 Qt DataVisualization 中为条形图创建数据时,以前的做法如下:

// Qt DataVisualization approach.
QBarDataRow *data = new QBarDataRow;
*data << 1.0f << 3.0f << 7.5f << 5.0f << 2.2f;

现在,操作方式如下:

// Qt Graphs approach.
QBarDataRow data;
data << 1.0f << 3.0f << 7.5f << 5.0f << 2.2f;
series->dataProxy()->addRow(data);

相机 API

由于 Q3DCamera 已被移除,其中所需的功能已迁移至其他位置。cameraPreset, cameraTargetPosition, cameraXRotation, cameraYRotation, cameraZoomLevel, wrapCameraXRotation, and wrapCameraYRotation 现已成为GraphsItem3D 的一部分。

主题 API

由于 Q3DTheme 已被移除,且 2D 与 3D 图表的主题设置已实现统一,其中部分必需函数已被迁移,其余函数可在替代类GraphsTheme 中找到。lightColor, ambientLightStrength, lightStrength, and shadowStrength 现基于GraphsItem3D 实现。

windowColor 已被移除。backgroundColor 现在实现了windowColor 原先的功能,而新函数plotAreaBackgroundColor 则取代了backgroundColor 此前提供的功能。

整个图表的通用配色方案现由color scheme 属性控制,而系列颜色则由theme 属性控制。如果未显式设置配色方案,则将遵循桌面主题(浅色/深色)。

枚举

在Qt Graphs 中,所有枚举均作为作用域枚举实现;例如,对于 Qt DataVisualization 中的QAbstract3DGraph::ShadowQualityLow ,Qt Graphs 中对应的枚举是QtGraphs3D::ShadowQuality::Low 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.