QGraphsTheme Class

QGraphsTheme 类为图形提供了一种可视化风格。更多

Header: #include <QGraphsTheme>
CMake: find_package(Qt6 REQUIRED COMPONENTS Graphs)
target_link_libraries(mytarget PRIVATE Qt6::Graphs)
qmake: QT += graphs
在 QML 中: GraphsTheme
继承:QObjectQQmlParserStatus

公共类型

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

信号

详细说明

指定影响整个图表的视觉属性。有多个内置主题,可按原样使用或自由修改。

可以使用UserDefined 枚举值从头开始创建主题。使用默认构造函数创建主题会生成一个新的用户定义主题。

自定义主题

默认主题为QtGreen ,但可以自定义每个属性。

下表列出了由主题控制的属性以及UserDefined 的默认值。

使用示例

创建一个内置主题,不做任何修改:

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::Automatic0如果平台配色方案可用,则背景颜色遵循平台配色方案。如果不可用,则使用浅色外观。
QGraphsTheme::ColorScheme::Light1背景颜色比文本颜色浅,即主题为浅色。
QGraphsTheme::ColorScheme::Dark2背景颜色比文本颜色深,即主题为深色。

另请参阅 Qt::ColorScheme

enum class QGraphsTheme::ColorStyle

梯度类型

常数描述
QGraphsTheme::ColorStyle::Uniform0对象以单一颜色渲染。使用的颜色在seriesColorssingleHighlightColormultiHighlightColor 属性中指定。
QGraphsTheme::ColorStyle::ObjectGradient1无论对象高度如何,每个对象都使用全渐变色渲染。使用的渐变色在seriesGradients,singleHighlightGradientmultiHighlightGradient 属性中指定。
QGraphsTheme::ColorStyle::RangeGradient2根据对象的高度及其在 Y 轴上的位置,使用全梯度的一部分为对象着色。使用的梯度在seriesGradients,singleHighlightGradientmultiHighlightGradient 属性中指定。

enum class QGraphsTheme::Theme

内置主题

常数描述
QGraphsTheme::Theme::QtGreen0以绿色为基色的灯光主题。
QGraphsTheme::Theme::QtGreenNeon1以绿色霓虹灯为基色的浅色主题。
QGraphsTheme::Theme::MixSeries2包含多种颜色的混合主题。
QGraphsTheme::Theme::OrangeSeries3以橙色为基色的主题。
QGraphsTheme::Theme::YellowSeries4以黄色为基色的主题。
QGraphsTheme::Theme::BlueSeries5以蓝色为基色的主题。
QGraphsTheme::Theme::PurpleSeries6以紫色为基色的主题。
QGraphsTheme::Theme::GreySeries7以灰色为基色的主题。
QGraphsTheme::Theme::UserDefined8用户自定义主题。更多信息,请参阅Customizing Theme

属性文档

axisXLabelFont : QFont

此属性用于保存 X 轴标签的字体。

访问功能:

QFont axisXLabelFont() const
void setAxisXLabelFont(const QFont &newAxisXLabelFont)

通知信号:

void axisXLabelFontChanged()

axisYLabelFont : QFont

此属性用于保存 Y 轴标签的字体。

访问功能:

QFont axisYLabelFont() const
void setAxisYLabelFont(const QFont &newAxisYLabelFont)

通知信号:

void axisYLabelFontChanged()

axisZLabelFont : QFont

该属性用于保存 Z 轴标签的字体。

访问功能:

QFont axisZLabelFont() const
void setAxisZLabelFont(const QFont &newAxisZLabelFont)

通知信号:

void axisZLabelFontChanged()

backgroundColor : QColor

该属性用于保存图表绘制视图的颜色。默认值取决于colorScheme

访问功能:

QColor backgroundColor() const
void setBackgroundColor(QColor newBackgroundColor)

Notifier 信号:

void backgroundColorChanged()

backgroundVisible : bool

该属性表示背景是否可见。

使用backgroundColor 的值绘制背景。默认值为true

访问功能:

bool isBackgroundVisible() const
void setBackgroundVisible(bool newBackgroundVisible)

Notifier 信号:

void backgroundVisibleChanged()

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 for 2D 中不起作用。

访问功能:

QGraphsTheme::ColorStyle colorStyle() const
void setColorStyle(QGraphsTheme::ColorStyle newColorStyle)

通知信号:

void colorStyleChanged(QGraphsTheme::ColorStyle type)

另请参阅 QGraphsTheme::ColorStyle

gridVisible : bool

该属性用于确定是否绘制网格线。

该值会影响所有网格线。默认值为true

访问功能:

bool isGridVisible() const
void setGridVisible(bool newGridVisibility)

通知信号:

void gridVisibleChanged()

labelBackgroundColor : QColor

该属性保存标签背景的颜色。

如果labelBackgroundVisiblefalse ,则该属性无效。默认值取决于colorScheme

访问功能:

QColor labelBackgroundColor() const
void setLabelBackgroundColor(QColor newLabelBackgroundColor)

Notifier 信号:

void labelBackgroundColorChanged()

labelBackgroundVisible : bool

该属性表示标签是以彩色背景还是全透明背景绘制。

labelBackgroundColor 值(包括 alpha)用于绘制背景。

有背景的标签按最长标签绘制,每个轴的尺寸相等,文本居中。无背景的标签按原样绘制,并根据其在图表中的位置向左或向右对齐。默认值为true

访问功能:

bool isLabelBackgroundVisible() const
void setLabelBackgroundVisible(bool newLabelBackgroundVisibility)

通知信号:

void labelBackgroundVisibleChanged()

labelBorderVisible : bool

此属性决定是否为有背景的标签绘制标签边框。

如果labelBackgroundVisiblefalse ,则无影响。默认值为true

访问功能:

bool isLabelBorderVisible() const
void setLabelBorderVisible(bool newLabelBorderVisibility)

Notifier 信号:

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)

Notifier 信号:

void labelTextColorChanged()

labelsVisible : bool

该属性决定是否绘制标签。

如果该属性为false ,则所有其他标签属性都不起作用。默认值为true

访问功能:

bool labelsVisible() const
void setLabelsVisible(bool newLabelsVisibility)

Notifier 信号:

void labelsVisibleChanged()

multiHighlightColor : QColor

该属性用于保存选定对象的高亮颜色。

如果selectionMode 设置了QtGraphs3D::SelectionFlag::RowQtGraphs3D::SelectionFlag::Column 标志,则会使用该属性。默认值取决于colorScheme

访问功能:

QColor multiHighlightColor() const
void setMultiHighlightColor(QColor newMultiHighlightColor)

Notifier 信号:

void multiHighlightColorChanged(QColor color)

multiHighlightGradient : QQuickGradient* const

该属性用于保存所选对象的高光渐变。

如果selectionMode 设置了QtGraphs3D::SelectionFlag::RowQtGraphs3D::SelectionFlag::Column 标志,则会使用该属性。默认值取决于colorScheme

通知信号:

void multiHighlightGradientQMLChanged()

plotAreaBackgroundColor : QColor

该属性用于设置图形绘制区域背景的颜色。默认值取决于colorScheme

访问功能:

QColor plotAreaBackgroundColor() const
void setPlotAreaBackgroundColor(QColor newBackgroundColor)

Notifier 信号:

void plotAreaBackgroundColorChanged()

plotAreaBackgroundVisible : bool

该属性用于确定绘图区域背景是否可见。

使用plotAreaBackgroundColor 的值绘制背景。默认值为true

访问功能:

bool isPlotAreaBackgroundVisible() const
void setPlotAreaBackgroundVisible(bool newBackgroundVisibility)

Notifier 信号:

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)

Notifier 信号:

void singleHighlightColorChanged(QColor color)

singleHighlightGradient : QQuickGradient* const

该属性用于保存选定对象的高光渐变。

如果selectionMode 设置了QtGraphs3D::SelectionFlag::Item 标志,则会使用该属性。默认值取决于colorScheme

Notifier 信号:

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)

成员函数文档

QList<QLinearGradient> QGraphsTheme::seriesGradients() const

返回主题使用的系列渐变的列表。

另请参阅 setSeriesGradients()。

void QGraphsTheme::setSeriesGradients(const QList<QLinearGradient> &newSeriesGradients)

newSeriesGradients 设置为主题的系列渐变。

另请参见 seriesGradients()。

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