QGraphsTheme Class
QGraphsTheme 类为图提供了视觉样式。更多内容...
| 头文件: | #include <QGraphsTheme> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Graphs) target_link_libraries(mytarget PRIVATE Qt6::Graphs) |
| qmake: | QT += graphs |
| 在 QML 中: | GraphsTheme |
| 继承自: | QObject 以及QQmlParserStatus |
- 所有成员列表(包括继承的成员)
- QGraphsTheme 属于Qt Graphs 的C++ 通用类。
公共类型
| enum class | ColorScheme { Automatic, Light, Dark } |
| enum class | ColorStyle { Uniform, ObjectGradient, RangeGradient } |
| enum class | Theme { QtGreen, QtGreenNeon, MixSeries, OrangeSeries, YellowSeries, …, UserDefined } |
属性
|
|
公共函数
| QGraphsLine | axisX() const |
| QFont | axisXLabelFont() const |
| QGraphsLine | axisY() const |
| QFont | axisYLabelFont() const |
| QGraphsLine | axisZ() const |
| QFont | axisZLabelFont() const |
| QColor | backgroundColor() const |
| QList<QColor> | borderColors() const |
| qreal | borderWidth() const |
| QGraphsTheme::ColorScheme | colorScheme() const |
| QGraphsTheme::ColorStyle | colorStyle() const |
| QGraphsLine | grid() const |
| bool | isBackgroundVisible() const |
| bool | isGridVisible() const |
| bool | isLabelBackgroundVisible() const |
| bool | isLabelBorderVisible() const |
| bool | isPlotAreaBackgroundVisible() const |
| QColor | labelBackgroundColor() const |
| QFont | labelFont() const |
| QColor | labelTextColor() const |
| bool | labelsVisible() const |
| QColor | multiHighlightColor() const |
| QColor | plotAreaBackgroundColor() const |
| QList<QColor> | seriesColors() const |
| QList<QLinearGradient> | seriesGradients() const |
| void | setAxisX(const QGraphsLine &newAxisX) |
| void | setAxisXLabelFont(const QFont &newAxisXLabelFont) |
| void | setAxisY(const QGraphsLine &newAxisY) |
| void | setAxisYLabelFont(const QFont &newAxisYLabelFont) |
| void | setAxisZ(const QGraphsLine &newAxisZ) |
| void | setAxisZLabelFont(const QFont &newAxisZLabelFont) |
| void | setBackgroundColor(QColor newBackgroundColor) |
| void | setBackgroundVisible(bool newBackgroundVisible) |
| void | setBorderColors(const QList<QColor> &newBorderColors) |
| void | setBorderWidth(qreal newBorderWidth) |
| void | setColorScheme(QGraphsTheme::ColorScheme newColorScheme) |
| void | setColorStyle(QGraphsTheme::ColorStyle newColorStyle) |
| void | setGrid(const QGraphsLine &newGrid) |
| void | setGridVisible(bool newGridVisibility) |
| void | setLabelBackgroundColor(QColor newLabelBackgroundColor) |
| void | setLabelBackgroundVisible(bool newLabelBackgroundVisibility) |
| void | setLabelBorderVisible(bool newLabelBorderVisibility) |
| void | setLabelFont(const QFont &newFont) |
| void | setLabelTextColor(QColor newLabelTextColor) |
| void | setLabelsVisible(bool newLabelsVisibility) |
| void | setMultiHighlightColor(QColor newMultiHighlightColor) |
| void | setPlotAreaBackgroundColor(QColor newBackgroundColor) |
| void | setPlotAreaBackgroundVisible(bool newBackgroundVisibility) |
| void | setSeriesColors(const QList<QColor> &newSeriesColors) |
| void | setSeriesGradients(const QList<QLinearGradient> &newSeriesGradients) |
| void | setSingleHighlightColor(QColor newSingleHighlightColor) |
| void | setTheme(QGraphsTheme::Theme newTheme, QGraphsTheme::ForceTheme force = ForceTheme::No) |
| QColor | singleHighlightColor() const |
| QGraphsTheme::Theme | theme() const |
信号
| void | axisXChanged() |
| void | axisXLabelFontChanged() |
| void | axisYChanged() |
| void | axisYLabelFontChanged() |
| void | axisZChanged() |
| void | axisZLabelFontChanged() |
| void | backgroundColorChanged() |
| void | backgroundVisibleChanged() |
| void | borderColorsChanged() |
| void | borderWidthChanged() |
| void | colorSchemeChanged() |
| void | colorStyleChanged(QGraphsTheme::ColorStyle type) |
| void | gridChanged() |
| void | gridVisibleChanged() |
| void | labelBackgroundColorChanged() |
| void | labelBackgroundVisibleChanged() |
| void | labelBorderVisibleChanged() |
| void | labelFontChanged() |
| void | labelTextColorChanged() |
| void | labelsVisibleChanged() |
| void | multiHighlightColorChanged(QColor color) |
| void | multiHighlightGradientQMLChanged() |
| void | plotAreaBackgroundColorChanged() |
| void | plotAreaBackgroundVisibleChanged() |
| void | seriesColorsChanged(const QList<QColor> &list) |
| void | singleHighlightColorChanged(QColor color) |
| void | singleHighlightGradientQMLChanged() |
| void | themeChanged(QGraphsTheme::Theme theme) |
详细说明
指定影响整个图形的视觉属性。有几种内置主题,既可以直接使用,也可以自由修改。
可以使用UserDefined 枚举值从头开始创建主题。使用默认构造函数创建主题将生成一个新的用户自定义主题。
自定义主题
默认主题为QtGreen ,但可以自定义每个属性。
下表列出了由主题控制的属性以及UserDefined 的默认值。
| 属性 | 默认值 |
|---|---|
| backgroundVisible | true |
| seriesColors | Qt::black |
| seriesGradients | QLinearGradient. 基本上是纯黑色。 |
| colorStyle | Uniform |
| labelFont | QFont |
| gridVisible | true |
| labelBackgroundVisible | true |
| labelBorderVisible | true |
| labelsVisible | true |
使用示例
创建一个无需任何修改的内置主题:
QGraphsTheme *theme = new QGraphsTheme();创建内置主题并修改部分属性:
QGraphsTheme *theme = new QGraphsTheme();
theme->setBackgroundVisible(false);
theme->setLabelBackgroundVisible(false);修改用户自定义主题。该主题的创建方式与前面的代码片段相同:
theme->setTheme(QGraphsTheme::Theme::UserDefined);
theme->setBackgroundColor(QColor(QRgb(0x99ca53)));
theme->setBackgroundVisible(true);
QList<QColor> colors = { QColor(QRgb(0x209fdf)) };
theme->setSeriesColors(colors);
theme->setColorStyle(QGraphsTheme::ColorStyle::Uniform);
theme->setLabelFont(QFont(QStringLiteral("Impact"), 35));
theme->setGridVisible(true);
auto gridline = theme->grid();
gridline.setMainColor(QColor(QRgb(0x99ca53)));
theme->setGrid(gridline);
theme->setLabelBackgroundColor(QColor(0xf6, 0xa6, 0x25, 0xa0));
theme->setLabelBackgroundVisible(true);
theme->setLabelBorderVisible(true);
theme->setLabelTextColor(QColor(QRgb(0x404044)));
theme->setMultiHighlightColor(QColor(QRgb(0x6d5fd5)));
theme->setSingleHighlightColor(QColor(QRgb(0xf6a625)));
theme->setBackgroundColor(QColor(QRgb(0xffffff)));在将主题应用于图后修改某些属性:
QQuickWidget quickWidget;
Q3DBarsWidgetItem bars;
bars.setWidget(&quickWidget);
bars.widget()->setMinimumSize(QSize(512, 512));
bars.activeTheme()->setTheme(QGraphsTheme::Theme::MixSeries);
QList<QColor> color = { QColor(Qt::red) };
bars.activeTheme()->setSeriesColors(color);
bars.activeTheme()->setSingleHighlightColor(Qt::yellow);成员类型文档
enum class QGraphsTheme::ColorScheme
表示图表的配色方案。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphsTheme::ColorScheme::Automatic | 0 | 如果平台配色方案可用,则背景颜色将遵循该方案。如果不可用,则使用“浅色”外观。 |
QGraphsTheme::ColorScheme::Light | 1 | 背景颜色比文本颜色浅,即主题为浅色。 |
QGraphsTheme::ColorScheme::Dark | 2 | 背景颜色比文字颜色深,即主题为深色。 |
另请参阅 Qt::ColorScheme 。
enum class QGraphsTheme::ColorStyle
梯度类型。
| 恒定 | 值 | 说明 |
|---|---|---|
QGraphsTheme::ColorStyle::Uniform | 0 | 对象以单一颜色呈现。所使用的颜色由seriesColors 、singleHighlightColor 和multiHighlightColor 属性指定。 |
QGraphsTheme::ColorStyle::ObjectGradient | 1 | 无论对象高度如何,每个对象均使用完整的渐变进行着色。所使用的渐变由seriesGradients 、singleHighlightGradient 和multiHighlightGradient 属性指定。 |
QGraphsTheme::ColorStyle::RangeGradient | 2 | 对象的着色使用完整渐变的一部分,该部分由对象的高度及其在 Y 轴上的位置决定。所使用的渐变由seriesGradients 、singleHighlightGradient 和multiHighlightGradient 属性指定。 |
enum class QGraphsTheme::Theme
内置主题。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphsTheme::Theme::QtGreen | 0 | 以绿色为基色的浅色主题。 |
QGraphsTheme::Theme::QtGreenNeon | 1 | 以绿色霓虹色为基调的浅色主题。 |
QGraphsTheme::Theme::MixSeries | 2 | 一种混合了多种颜色的主题。 |
QGraphsTheme::Theme::OrangeSeries | 3 | 以橙色为基色的主题。 |
QGraphsTheme::Theme::YellowSeries | 4 | 以黄色为基色的主题。 |
QGraphsTheme::Theme::BlueSeries | 5 | 以蓝色为底色的主题。 |
QGraphsTheme::Theme::PurpleSeries | 6 | 以紫色为基色的主题。 |
QGraphsTheme::Theme::GreySeries | 7 | 以灰色为基色的主题。 |
QGraphsTheme::Theme::UserDefined | 8 | 用户自定义主题。有关详细信息,请参阅Customizing Theme 。 |
属性文档
axisX : QGraphsLine
图表中X轴线和标签的样式属性。
访问函数:
| QGraphsLine | axisX() const |
| void | setAxisX(const QGraphsLine &newAxisX) |
通知信号:
| void | axisXChanged() |
axisXLabelFont : QFont
此属性用于指定x轴标签所使用的字体。
访问函数:
| QFont | axisXLabelFont() const |
| void | setAxisXLabelFont(const QFont &newAxisXLabelFont) |
通知信号:
| void | axisXLabelFontChanged() |
axisY : QGraphsLine
图表中Y轴线和标签的样式属性。
访问函数:
| QGraphsLine | axisY() const |
| void | setAxisY(const QGraphsLine &newAxisY) |
通知器信号:
| void | axisYChanged() |
axisYLabelFont : QFont
此属性用于指定 y 轴标签所使用的字体。
访问函数:
| QFont | axisYLabelFont() const |
| void | setAxisYLabelFont(const QFont &newAxisYLabelFont) |
通知信号:
| void | axisYLabelFontChanged() |
axisZ : QGraphsLine
图表的 Z 轴线和标签样式属性。
访问函数:
| QGraphsLine | axisZ() const |
| void | setAxisZ(const QGraphsLine &newAxisZ) |
通知信号:
| void | axisZChanged() |
axisZLabelFont : QFont
此属性用于指定 Z 轴标签所使用的字体。
访问函数:
| QFont | axisZLabelFont() const |
| void | setAxisZLabelFont(const QFont &newAxisZLabelFont) |
通知信号:
| void | axisZLabelFontChanged() |
backgroundColor : QColor
该属性存储用于绘制图形的视图的颜色。默认值取决于colorScheme 。
访问函数:
| QColor | backgroundColor() const |
| void | setBackgroundColor(QColor newBackgroundColor) |
通知器信号:
| void | backgroundColorChanged() |
backgroundVisible : bool
该属性控制背景是否可见。
背景的绘制基于backgroundColor 的值。默认值为true 。
访问函数:
| bool | isBackgroundVisible() const |
| void | setBackgroundVisible(bool newBackgroundVisible) |
通知器信号:
| void | backgroundVisibleChanged() |
[read-only] baseColors : const QQmlListProperty<QQuickGraphsColor>
使用ColorStyle::Uniform 配色方案时,该系列将采用的基础颜色列表。
[read-only] baseGradients : const QQmlListProperty<QQuickGradient>
使用ColorStyle::RangeGradient 颜色样式时,该系列所使用的基底渐变列表。
borderColors : QList<QColor>
该属性存储了图中所有对象将使用的边框颜色列表,按系列分别列出。
如果系列数量多于颜色数量,则颜色列表将循环循环,并从列表中的第一种颜色开始重新排列。
如果colorStyle 不是Uniform,则该属性不会立即生效。
访问函数:
| QList<QColor> | borderColors() const |
| void | setBorderColors(const QList<QColor> &newBorderColors) |
通知信号:
| void | borderColorsChanged() |
borderWidth : qreal
该属性用于指定图形中边框的宽度(如有)。默认值为1.0 。
访问函数:
| qreal | borderWidth() const |
| void | setBorderWidth(qreal newBorderWidth) |
通知信号:
| void | borderWidthChanged() |
colorScheme : QGraphsTheme::ColorScheme
当前图表的配色方案。
访问函数:
| QGraphsTheme::ColorScheme | colorScheme() const |
| void | setColorScheme(QGraphsTheme::ColorScheme newColorScheme) |
通知信号:
| void | colorSchemeChanged() |
另请参阅 Qt::ColorScheme 。
colorStyle : QGraphsTheme::ColorStyle
图表颜色的样式。QGraphsTheme::ColorStyle 枚举值之一。
可以通过在系列中显式设置Abstract3DSeries.colorStyle 来覆盖此值。
注意: 在Qt Graphs 的 2D 模式下,此 属性无效。
访问函数:
| QGraphsTheme::ColorStyle | colorStyle() const |
| void | setColorStyle(QGraphsTheme::ColorStyle newColorStyle) |
通知信号:
| void | colorStyleChanged(QGraphsTheme::ColorStyle type) |
另请参阅 QGraphsTheme::ColorStyle 。
grid : QGraphsLine
图表的网格线样式属性。
访问函数:
| QGraphsLine | grid() const |
| void | setGrid(const QGraphsLine &newGrid) |
通知器信号:
| void | gridChanged() |
gridVisible : bool
该属性控制是否绘制网格线。
该值影响所有网格线。默认值为true 。
访问函数:
| bool | isGridVisible() const |
| void | setGridVisible(bool newGridVisibility) |
通知信号:
| void | gridVisibleChanged() |
labelBackgroundColor : QColor
该属性控制标签背景的颜色。
若labelBackgroundVisible 的值为false ,则此属性无效。默认值取决于colorScheme 。
访问函数:
| QColor | labelBackgroundColor() const |
| void | setLabelBackgroundColor(QColor newLabelBackgroundColor) |
通知器信号:
| void | labelBackgroundColorChanged() |
labelBackgroundVisible : bool
无论标签是绘制在彩色背景上还是完全透明的背景上,此属性均有效。
绘制背景时将使用labelBackgroundColor 的值(包括透明度)。
带有背景的标签会根据最长的标签,在每个坐标轴上绘制为等大小的区域,且文本在其中居中显示。没有背景的标签则按原样绘制,并根据其在图表中的位置向左或向右对齐。默认值为true 。
访问函数:
| bool | isLabelBackgroundVisible() const |
| void | setLabelBackgroundVisible(bool newLabelBackgroundVisibility) |
通知信号:
| void | labelBackgroundVisibleChanged() |
labelBorderVisible : bool
该属性用于控制是否为带有背景的标签绘制边框。
若labelBackgroundVisible 的值为false ,则此属性无效。默认值为true 。
访问函数:
| bool | isLabelBorderVisible() const |
| void | setLabelBorderVisible(bool newLabelBorderVisibility) |
通知信号:
| void | labelBorderVisibleChanged() |
labelFont : QFont
此属性用于指定标签所用的字体。
访问函数:
| QFont | labelFont() const |
| void | setLabelFont(const QFont &newFont) |
通知信号:
| void | labelFontChanged() |
labelTextColor : QColor
该属性用于指定标签所用字体的颜色。
如果坐标轴已显式指定了labelTextColor ,则此属性将不起作用。
默认值取决于colorScheme 。
访问函数:
| QColor | labelTextColor() const |
| void | setLabelTextColor(QColor newLabelTextColor) |
通知信号:
| void | labelTextColorChanged() |
labelsVisible : bool
该属性决定是否绘制标签。
如果该属性值为false ,则所有其他标签属性均无效。默认值为true 。
访问函数:
| bool | labelsVisible() const |
| void | setLabelsVisible(bool newLabelsVisibility) |
通知信号:
| void | labelsVisibleChanged() |
multiHighlightColor : QColor
该属性用于存储所选对象的高亮颜色。
当selectionMode 设置了QtGraphs3D::SelectionFlag::Row 或QtGraphs3D::SelectionFlag::Column 标志时,该属性才会生效。默认值取决于colorScheme 。
访问函数:
| QColor | multiHighlightColor() const |
| void | setMultiHighlightColor(QColor newMultiHighlightColor) |
通知器信号:
| void | multiHighlightColorChanged(QColor color) |
multiHighlightGradient : QQuickGradient*
该属性用于存储所选对象的高亮渐变。
当selectionMode 设置了QtGraphs3D::SelectionFlag::Row 或QtGraphs3D::SelectionFlag::Column 标志时使用。默认值取决于colorScheme 。
通知器信号:
| void | multiHighlightGradientQMLChanged() |
plotAreaBackgroundColor : QColor
该属性用于设置图形绘图区域背景的颜色。默认值取决于colorScheme 。
访问函数:
| QColor | plotAreaBackgroundColor() const |
| void | setPlotAreaBackgroundColor(QColor newBackgroundColor) |
通知信号:
| void | plotAreaBackgroundColorChanged() |
plotAreaBackgroundVisible : bool
该属性控制绘图区域的背景是否可见。
背景的绘制基于plotAreaBackgroundColor 的值。默认值为true 。
访问函数:
| bool | isPlotAreaBackgroundVisible() const |
| void | setPlotAreaBackgroundVisible(bool newBackgroundVisibility) |
通知信号:
| void | plotAreaBackgroundVisibleChanged() |
seriesColors : QList<QColor>
该属性存储了图中所有对象将使用的基色列表,按系列逐个列出。
如果系列数量多于颜色数量,则颜色列表将循环,并从列表中的第一个颜色开始重新循环。
如果 `colorStyle ` 不是 `Uniform`,则此属性不会立即生效。
可以通过在系列中显式设置baseColor 来覆盖此值。
访问函数:
| QList<QColor> | seriesColors() const |
| void | setSeriesColors(const QList<QColor> &newSeriesColors) |
通知信号:
| void | seriesColorsChanged(const QList<QColor> &list) |
singleHighlightColor : QColor
该属性存储所选对象的高亮颜色。
当selectionMode 设置了QtGraphs3D::SelectionFlag::Item 标志时,该属性才会生效。其默认值取决于colorScheme 。
访问函数:
| QColor | singleHighlightColor() const |
| void | setSingleHighlightColor(QColor newSingleHighlightColor) |
通知信号:
| void | singleHighlightColorChanged(QColor color) |
singleHighlightGradient : QQuickGradient*
该属性用于存储所选对象的高亮渐变。
当selectionMode 设置了QtGraphs3D::SelectionFlag::Item 标志时使用。默认值取决于colorScheme 。
通知信号:
| void | singleHighlightGradientQMLChanged() |
theme : QGraphsTheme::Theme
主题的类型。如果未设置类型,则类型默认为GraphsTheme.Theme.QtGreen 。在项构建完成后更改主题类型,将把主题的所有其他属性更改为预定义主题所指定的值。更改图中当前主题的类型,也会将所有关联的系列重置为使用新主题。
访问函数:
| QGraphsTheme::Theme | theme() const |
| void | setTheme(QGraphsTheme::Theme newTheme, QGraphsTheme::ForceTheme force = ForceTheme::No) |
通知器信号:
| void | themeChanged(QGraphsTheme::Theme theme) |
[read-only] themeChildren : const QQmlListProperty<QObject>
主题子对象的列表。这是一个默认属性,允许在 QML 的 Theme 元素中指定子对象,而无需显式使用 children 属性名。
成员函数文档
QList<QLinearGradient> QGraphsTheme::seriesGradients() const
返回主题所使用的系列渐变列表。
另请参阅 setSeriesGradients()。
void QGraphsTheme::setSeriesGradients(const QList<QLinearGradient> &newSeriesGradients)
将newSeriesGradients 设置为主题的系列渐变。
另请参阅 seriesGradients()。
© 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.