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.
创建小部件应用程序
要在 widget 应用程序中创建图形,请将代码从:
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
- Q3D 对象
- Q3D 主题
删除的应用程序接口
- hasContext
- 支持阴影
- 反射
- 反射
已更改的 API
- 优化提示
- 渲染模式
- 渲染为图像
- 颜色渐变和颜色渐变停止
- 主题颜色
- 数据 API
- 相机 API
- 主题 API
- 枚举
优化提示
Legacy
现在是在 QtDataVisualization 中 的模式。 使用实例化,应用于所有支持实例化的目标。OptimizationDefault
Default
渲染模式
RenderDirectToBackground_NoClear
已被移除,因为它在 Qt 6 的 QtDataVisualization 中已经过时。
renderToImage
renderToImage
现在返回 而不是 ,并且不再将 作为参数。QSharedPointer<QQuickItemGrabResult>
QImage
msaaSamples
ColorGradient 和 ColorGradientStop
ColorGradient
现在是 和 。Gradient
ColorGradientStop
GradientStop
主题颜色
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 XML DataVisualization 中的QAbstract3DGraph::ShadowQualityLow
,Qt Graphs 中的相应枚举是QtGraphs3D::ShadowQuality::Low
。
© 2025 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.