QLegend Class

QLegend 类用于显示图表的图例。更多

Header: #include <QLegend>
In QML: Legend
Inherits: QGraphicsWidget

公共类型

enum MarkerShape { MarkerShapeDefault, MarkerShapeRectangle, MarkerShapeCircle, MarkerShapeRotatedRectangle, MarkerShapeTriangle, …, MarkerShapeFromSeries }

属性

公共功能

virtual ~QLegend()
Qt::Alignment alignment() const
void attachToChart()
QColor borderColor()
QBrush brush() const
QColor color()
void detachFromChart()
QFont font() const
bool isAttachedToChart()
bool isBackgroundVisible() const
(since 6.2) bool isInteractive() const
QBrush labelBrush() const
QColor labelColor() const
QLegend::MarkerShape markerShape() const
QList<QLegendMarker *> markers(QAbstractSeries *series = nullptr) const
QPen pen() const
bool reverseMarkers()
void setAlignment(Qt::Alignment alignment)
void setBackgroundVisible(bool visible = true)
void setBorderColor(QColor color)
void setBrush(const QBrush &brush)
void setColor(QColor color)
void setFont(const QFont &font)
(since 6.2) void setInteractive(bool interactive)
void setLabelBrush(const QBrush &brush)
void setLabelColor(QColor color)
void setMarkerShape(QLegend::MarkerShape shape)
void setPen(const QPen &pen)
void setReverseMarkers(bool reverseMarkers = true)
void setShowToolTips(bool show)
bool showToolTips() const

信号

(since 6.2) void attachedToChartChanged(bool attached)
void backgroundVisibleChanged(bool visible)
void borderColorChanged(QColor color)
void colorChanged(QColor color)
void fontChanged(QFont font)
void labelColorChanged(QColor color)
void markerShapeChanged(QLegend::MarkerShape shape)
void reverseMarkersChanged(bool reverseMarkers)
void showToolTipsChanged(bool showToolTips)

详细说明

图例是显示图表图例的图形对象。当序列发生变化时,图例状态会通过QChart 更新。默认情况下,图例附在图表上,但也可以将其分离,使其独立于图表布局。图例对象不能创建或删除,但可以通过QChart 类引用。

另请参阅 QChart

成员类型文档

enum QLegend::MarkerShape

该枚举描述了渲染图例标记项时使用的形状。

常量说明
QLegend::MarkerShapeDefault0标记使用QLegend 确定的默认形状。该值仅支持单个QLegendMarker 项。
QLegend::MarkerShapeRectangle1使用矩形标记。标记大小由字体大小决定。
QLegend::MarkerShapeCircle2使用圆形标记。标记大小由字体大小决定。
QLegend::MarkerShapeRotatedRectangle4使用旋转矩形标记。标记大小由字体大小决定。
QLegend::MarkerShapeTriangle5使用三角形标记。标记大小由字体大小决定。
QLegend::MarkerShapeStar6使用星形标记。标记大小由字体大小决定。
QLegend::MarkerShapePentagon7使用五角形标记。标记大小由字体大小决定。
QLegend::MarkerShapeFromSeries3标记形状由序列决定。如果是散点序列,图例标记看起来像散点,大小与散点相同。如果是折线或样条线序列,图例标记看起来就像折线的一小段。对于其他序列类型,则显示矩形标记。如果为序列指定了lightMarker ,则将显示lightMarker ,其大小由序列标记大小决定。

另请参阅 markerShape

属性文档

alignment : Qt::Alignment

图例如何与图表对齐。

可以是Qt::AlignTop,Qt::AlignBottom,Qt::AlignLeft,Qt::AlignRight 。如果设置了多个标志,结果将是未定义的。

访问功能:

Qt::Alignment alignment() const
void setAlignment(Qt::Alignment alignment)

backgroundVisible : bool

该属性用于确定图例背景是否可见。

访问功能:

bool isBackgroundVisible() const
void setBackgroundVisible(bool visible = true)

通知信号:

void backgroundVisibleChanged(bool visible)

borderColor : QColor

该属性用于保存图例的线条颜色。

访问功能:

QColor borderColor()
void setBorderColor(QColor color)

通知信号:

void borderColorChanged(QColor color)

color : QColor

该属性用于保存图例的背景(刷子)颜色。

如果更改了图例的颜色,图例画笔的样式将设置为Qt::SolidPattern

访问功能:

QColor color()
void setColor(QColor color)

通知信号:

void colorChanged(QColor color)

font : QFont

该属性用于保存图例所用标记的字体。

访问功能:

QFont font() const
void setFont(const QFont &font)

通知信号:

void fontChanged(QFont font)

labelColor : QColor

该属性保存用于绘制标签的笔刷颜色。

访问功能:

QColor labelColor() const
void setLabelColor(QColor color)

Notifier 信号:

void labelColorChanged(QColor color)

markerShape : MarkerShape

图例标记的默认形状。默认值为MarkerShapeRectangle

访问功能:

QLegend::MarkerShape markerShape() const
void setMarkerShape(QLegend::MarkerShape shape)

通知信号:

void markerShapeChanged(QLegend::MarkerShape shape)

reverseMarkers : bool

该属性表示图例中的标记是否使用相反顺序。

该属性默认为false

访问功能:

bool reverseMarkers()
void setReverseMarkers(bool reverseMarkers = true)

Notifier 信号:

void reverseMarkersChanged(bool reverseMarkers)

showToolTips : bool

当文本被截断时,是否显示工具提示。

该属性默认为false

访问功能:

bool showToolTips() const
void setShowToolTips(bool show)

Notifier 信号:

void showToolTipsChanged(bool showToolTips)

成员函数 文档

[virtual noexcept] QLegend::~QLegend()

销毁图例对象。图例始终归QChart 所有,因此应用程序不应调用此函数。

void QLegend::attachToChart()

将图例附加到图表。图表可调整图例的布局。

[signal, since 6.2] void QLegend::attachedToChartChanged(bool attached)

当图例attached 或从图表中分离时,将发出该信号。

该函数在 Qt 6.2 中引入。

[signal] void QLegend::backgroundVisibleChanged(bool visible)

当图例背景的可见性变为visible 时,将发出该信号。

注: 属性backgroundVisible 的通知信号。

[signal] void QLegend::borderColorChanged(QColor color)

当图例背景的边框颜色更改为color 时,将发出该信号。

注: 属性borderColor 的通知信号。

QBrush QLegend::brush() const

返回图例使用的笔刷。

另请参见 setBrush()。

[signal] void QLegend::colorChanged(QColor color)

当图例背景颜色更改为color 时,将发出该信号。

注: 属性color 的通知信号。

void QLegend::detachFromChart()

从图表中分离图例。图表将不再调整图例的布局。

[signal] void QLegend::fontChanged(QFont font)

当图例标记的字体更改为font 时,将发出该信号。

注: 属性font 的通知信号。

bool QLegend::isAttachedToChart()

如果图例附在图表上,则返回true

bool QLegend::isBackgroundVisible() const

返回图例背景的可见性。

注: 属性backgroundVisible 的获取函数。

[since 6.2] bool QLegend::isInteractive() const

返回图例分离时是否可以使用鼠标拖动或调整大小。

此函数在 Qt 6.2 中引入。

另请参阅 QLegend::setInteractive()。

QBrush QLegend::labelBrush() const

返回用于绘制标签的笔刷。

另请参阅 setLabelBrush()。

[signal] void QLegend::labelColorChanged(QColor color)

当用于绘制图例标签的笔刷颜色更改为color 时,将发出该信号。

注: 用于labelColor 属性的通知信号。

QList<QLegendMarker *> QLegend::markers(QAbstractSeries *series = nullptr) const

返回图例中的标记列表。可以通过指定series 来筛选列表,并返回标记。

QPen QLegend::pen() const

返回图例使用的笔。

另请参见 setPen()。

[signal] void QLegend::reverseMarkersChanged(bool reverseMarkers)

当图例中的标记使用相反顺序更改为reverseMarkers 时,将发出该信号。

注: 属性reverseMarkers 的通知信号。

void QLegend::setBackgroundVisible(bool visible = true)

将图例背景的可见性设置为visible

注: 属性backgroundVisible 的设置函数。

另请参阅 isBackgroundVisible() 。

void QLegend::setBrush(const QBrush &brush)

设置用于绘制图例背景的brush

另请参阅 brush()。

[since 6.2] void QLegend::setInteractive(bool interactive)

interactivetrue 且图例被分离时,图例可以用鼠标移动和调整大小,其方式与窗口类似。

将图例拖离图表边缘,图例将自动附加到该边缘。双击附着的图例将使其分离。默认情况下,false

此功能在 Qt 6.2 中引入。

另请参阅 QLegend::isInteractive()。

void QLegend::setLabelBrush(const QBrush &brush)

将用于绘制图例标签的笔刷设置为brush

另请参阅 labelBrush() 。

void QLegend::setPen(const QPen &pen)

设置用于绘制图例边框的pen

另请参阅 pen()。

void QLegend::setShowToolTips(bool show)

showtrue 时,如果标签本身被省略,则当鼠标悬停在图例标签上时,图例标签将显示工具提示。默认情况下,false

注: 属性showToolTips 的设置函数。

另请参阅 showToolTips() 。

bool QLegend::showToolTips() const

返回图例标签被省略时是否显示工具提示。

注: 属性 showToolTips 的获取函数。

另请参阅 setShowToolTips().

[signal] void QLegend::showToolTipsChanged(bool showToolTips)

当工具提示的可见性更改为showToolTips 时,将发出该信号。

注: 属性showToolTips 的通知信号。

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