Qt图例类QLegend详解 |
您所在的位置:网站首页 › 如何绘制饼图图例 › Qt图例类QLegend详解 |
概述
在Qt绘制图表时,图例并不是由QChart类所管理的,而是交给单独的QLegend类。 QLegend类负责图例的绘制(包括颜色、线型、字体等),它与图表类QChart的关系是attach和detach。 实例 参考官方实例: X:\Qt\Qt5.9.0\Examples\Qt-5.9\charts\legend 或者: https://doc.qt.io/qt-5/qtcharts-legend-example.html
运行效果: 先看看效果: 点击Toggle Bold按钮后,图例中显示的数据系列的名称,变为粗体: 对应的功能代码为: QFont font = m_chart->legend()->font(); font.setBold(!font.bold()); m_chart->legend()->setFont(font); 设置图例标签是否斜体同样的,设置为斜体: 对应的功能代码为: QFont font = m_chart->legend()->font(); font.setItalic(!font.italic()); m_chart->legend()->setFont(font); 设置图例标签字体大小改变字体大小为6: 对应的功能代码为: QFont font = m_chart->legend()->font(); font.setPointSizeF(m_fontSize->value()); m_chart->legend()->setFont(font); 图例的对齐格式:功能代码: void MainWidget::setLegendAlignment() { QPushButton *button = qobject_cast(sender()); switch (m_chart->legend()->alignment()) { case Qt::AlignTop: m_chart->legend()->setAlignment(Qt::AlignLeft); if (button) button->setText("Align (Left)"); break; case Qt::AlignLeft: m_chart->legend()->setAlignment(Qt::AlignBottom); if (button) button->setText("Align (Bottom)"); break; case Qt::AlignBottom: m_chart->legend()->setAlignment(Qt::AlignRight); if (button) button->setText("Align (Right)"); break; default: if (button) button->setText("Align (Top)"); m_chart->legend()->setAlignment(Qt::AlignTop); break; } }效果: 底部显示、代码: legend->detachFromChart(); m_chart->legend()->setBackgroundVisible(true); m_chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 128))); m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192)));效果: 还可以自由地移动图例的位置。功能代码: m_chart->legend()->setGeometry(QRectF(m_legendPosX->value(), m_legendPosY->value(), m_legendWidth->value(), m_legendHeight->value()));返回到附着效果: legend->attachToChart(); legend->setBackgroundVisible(false);
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |