本页内容

Qt Charts C++ Classes

用于Qt Charts API的C++类。更多内容...

自 6.10 起,此模块已被弃用。我们强烈建议不要在新代码中使用它。

类

详细说明

请改用QtGraphs 。

Charts API 基于 Qt Graphics View Framework 构建。可通过QChart 类将图表以QGraphicsWidget 的形式显示。此外,还有一个基于QWidget 的便捷类QChartView 。这些类使我们能够快速将Qt Charts 作为普通的 Qt 控件使用。

注意: 自 Qt 6.10 起,Qt Charts 模块 已被废弃。对于新项目,请改用 Qt Graphs 模块。Qt Graphs 利用各平台的原生渲染后端(macOS 上的 Metal、Windows 上的 DirectX、Linux 上的 OpenGL 或 Vulkan)为 2D 和 3D 图表实现硬件加速渲染。Qt Graphs 采用 Qt Quick Shapes 用于 2D 图表渲染,而非Qt Charts 模块所使用的过时 Qt Graphics View Framework。若要从Qt Charts 迁移到Qt Graphs 模块,请参阅Qt Graphs Migration fromQt Charts 。

要使用 Qt 模块的 C++ API,需要将项目链接到该模块的库,可以是直接链接,也可以是通过其他依赖项间接链接。一些构建工具对此提供了专门的支持,包括 CMake 和 qmake。

注意: 使用Qt Creator 的“Qt Quick ”应用程序向导创建的项目 基于Qt Quick 2 模板,该模板默认使用QGuiApplication 。项目中所有此类QGuiApplication 实例都必须替换为QApplication ,因为该模块依赖于 Qt 的Graphics View Framework进行渲染。

若要在 CMake 中使用该模块,请使用find_package() 命令在Qt6 包中定位所需的模块组件:

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

若要使用该模块通过 qmake 进行构建,请在项目的 .pro 文件中将该模块作为QT 变量的值添加:

QT += charts

每种图表类型都由QAbstractSeries 的派生类表示。要创建图表,用户必须使用相关系列类的实例,并将其添加到QChart 实例中。

QLineSeries* series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
...
chartView->chart()->addSeries(series);
chartView->chart()->createDefaultAxes();

© 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.