目录
Qchart简介
QChartView 简介
QAbstractAxis 简介
QAbstractSeries 简介
Qchart
Public Types
Properties属性
Public Functions
QAbstractSeries
Public Types
Properties
Public Functions
Signals信号
QAbstractAxis
Properties
Public Functions
主题设置例子
Qchart简介
QChart是一个QGraphicsWidget,可以在QGraphicsScene中显示。它管理不同类型的系列和其他图表相关对象的图形表示,如图例和轴。为了简单地显示布局中的图表,可以使用方便类QChartView来代替QChart。此外,线、样条、面积和散点系列可以通过使用QPolarChart类显示为极坐标图。
QChartView 简介
chart view不需要QGraphicsScene对象即可工作。若要在现有QGraphicsScene中显示图表,应改用QChart或QPolarChart类。
QAbstractAxis 简介
QAbstractAxis类是用于专门轴类的基类。每个serial可以绑定到一个或多个水平轴和垂直轴,但不支持将导致不同域的混合轴类型,例如在同一方向上指定QValueAxis和QLogValueAxis。可以单独控制各种轴元素(如轴线、标题、标签、网格线和阴影)的属性和可见性。
QAbstractSeries 简介
QAbstractSeries类是所有Qt Chart系列的基类、通常,使用序列类型特定的继承类来代替基类。
Qchart
Public Types
enum | AnimationOption { NoAnimation, GridAxisAnimations, SeriesAnimations, AllAnimations } 此枚举描述图表中启用的动画 |
flags | AnimationOptions |
enum | ChartTheme { ChartThemeLight, ChartThemeBlueCerulean, ChartThemeDark, ChartThemeBrownSand, ChartThemeBlueNcs, …, ChartThemeQt } 此枚举描述图表使用的主题。 |
enum | ChartType { ChartTypeUndefined, ChartTypeCartesian, ChartTypePolar } 此枚举描述图表类型。 |
此枚举描述图表中启用的动画
Constant | Value | Description |
---|---|---|
QChart::NoAnimation | 0x0 | 图表中禁用动画。这是默认值。 |
QChart::GridAxisAnimations | 0x1 | 在图表中启用网格轴动画。 |
QChart::SeriesAnimations | 0x2 | 在图表中启用系列动画。 |
QChart::AllAnimations | 0x3 | 图表中的所有动画类型都已启用。 |
Constant常数 | Value价值 | Description说明 |
---|---|---|
QChart::ChartThemeLight | 0 | The light theme, which is the default theme.灯光主题,这是默认主题。 |
QChart::ChartThemeBlueCerulean | 1 | The cerulean blue theme.天蓝色的主题。 |
QChart::ChartThemeDark | 2 | The dark theme.黑暗的主题 |
QChart::ChartThemeBrownSand | 3 | The sand brown theme.沙棕色的主题。 |
QChart::ChartThemeBlueNcs | 4 | The natural color system (NCS) blue theme.自然色系统(NCS)蓝色主题。 |
QChart::ChartThemeHighContrast | 5 | The high contrast theme.高对比度主题。 |
QChart::ChartThemeBlueIcy | 6 | The icy blue theme.冰蓝色的主题。 |
QChart::ChartThemeQt | 7 | The Qt theme.Qt主题 |
Constant常数 | Value价值 | Description说明 |
---|---|---|
QChart::ChartTypeUndefined | 0 | The chart type is not defined.未定义图表类型。 |
QChart::ChartTypeCartesian | 1 | A cartesian chart.笛卡尔图 |
QChart::ChartTypePolar | 2 | A polar chart.极地图。 |
Properties属性
- animationDuration : int 此属性保存图表的动画持续时间。
- animationEasingCurve : QEasingCurve 此属性保存图表动画的缓动曲线。
- animationOptions : QChart::AnimationOptions 此属性保存图表的动画选项。
- backgroundRoundness : qreal 此属性保存图表背景角处的圆角直径。
- backgroundVisible : bool 此属性保存图表背景是否可见
- chartType : const QChart::ChartType 此属性保存图表是笛卡尔图还是极坐标图。
- dropShadowEnabled : bool 此属性保存是否启用背景投影效果。
- locale : QLocale 此属性保存用于格式化各种图表标签的区域设置
- localizeNumbers : bool 此属性保存数字是否本地化。
- margins : QMargins 此属性保存图表矩形的边缘和绘图区域之间允许的最小边距。
- plotArea : QRectF 此属性保存在其中绘制图表的矩形。
- plotAreaBackgroundVisible : bool 此属性用于保存图表打印区域背景是否可见。
- theme : QChart::ChartTheme 此属性保存用于图表的主题
- title : QString 此属性保存图表的标题。
Public Functions
QChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags()) | |
virtual | ~QChart() |
void | addAxis(QAbstractAxis *axis, Qt::Alignment alignment) 将轴轴添加到按对齐方式指定对齐的图表中。图表获得轴的所有权。 |
void | addSeries(QAbstractSeries *series) 将Series添加到图表中并获取其所有权。 |
int | animationDuration() const |
QEasingCurve | animationEasingCurve() const |
QChart::AnimationOptions | animationOptions() const |
QList<QAbstractAxis *> | axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = nullptr) const 返回附加到系列的轴,其方向由方向指定。如果未指定系列,则返回添加到图表中的具有指定方向的所有轴。 |
QBrush | backgroundBrush() const 获取用于绘制图表区域背景的画笔。 |
QPen | backgroundPen() const 获取用于绘制图表区域背景的笔。 |
qreal | backgroundRoundness() const |
QChart::ChartType | chartType() const |
void | createDefaultAxes() |
bool | isBackgroundVisible() const |
bool | isDropShadowEnabled() const |
bool | isPlotAreaBackgroundVisible() const |
bool | isZoomed() 如果任何系列具有缩放域,则返回 |
QLegend * | legend() const 返回图表的图例对象。所有权留在图表中。 |
QLocale | locale() const |
bool | localizeNumbers() const |
QPointF | mapToPosition(const QPointF &value, QAbstractSeries *series = nullptr) |
QPointF | mapToValue(const QPointF &position, QAbstractSeries *series = nullptr) |
QMargins | margins() const |
QRectF | plotArea() const |
QBrush | plotAreaBackgroundBrush() const |
QPen | plotAreaBackgroundPen() const |
void | removeAllSeries() |
void | removeAxis(QAbstractAxis *axis) |
void | removeSeries(QAbstractSeries *series) |
void | scroll(qreal dx, qreal dy) 按dx和dy指定的距离滚动图表的可见区域。 |
QList<QAbstractSeries *> | series() const |
void | setAnimationDuration(int msecs) |
void | setAnimationEasingCurve(const QEasingCurve &curve) |
void | setAnimationOptions(QChart::AnimationOptions options) |
void | setBackgroundBrush(const QBrush &brush) |
void | setBackgroundPen(const QPen &pen) |
void | setBackgroundRoundness(qreal diameter) |
void | setBackgroundVisible(bool visible = true) |
void | setDropShadowEnabled(bool enabled = true) |
void | setLocale(const QLocale &locale) |
void | setLocalizeNumbers(bool localize) |
void | setMargins(const QMargins &margins) |
void | setPlotArea(const QRectF &rect) |
void | setPlotAreaBackgroundBrush(const QBrush &brush) |
void | setPlotAreaBackgroundPen(const QPen &pen) |
void | setPlotAreaBackgroundVisible(bool visible = true) |
void | setTheme(QChart::ChartTheme theme) |
void | setTitle(const QString &title) |
void | setTitleBrush(const QBrush &brush) |
void | setTitleFont(const QFont &font) |
QChart::ChartTheme | theme() const |
QString | title() const |
QBrush | titleBrush() const |
QFont | titleFont() const |
void | zoom(qreal factor) |
void | zoomIn() |
void | zoomIn(const QRectF &rect) |
void | zoomOut() |
void | zoomReset() |
QAbstractSeries
QAbstractSeries类是所有Qt Chart系列的基类。通常,使用序列类型特定的继承类来代替基类。
Header: | #include <QAbstractSeries> |
Instantiated By: | AbstractSeries |
Inherits: | QObject |
Inherited By: | QAbstractBarSeries, QAreaSeries, QBoxPlotSeries, QCandlestickSeries, QPieSeries, and QXYSeries |
Public Types
enum | SeriesType { SeriesTypeLine, SeriesTypeArea, SeriesTypeBar, SeriesTypeStackedBar, SeriesTypePercentBar, …, SeriesTypeCandlestick } |
Constant常数 | Value价值 | Description说明 |
---|---|---|
QAbstractSeries::SeriesTypeLine | 0 | A line chart.折线图。 |
QAbstractSeries::SeriesTypeArea | 1 | An area chart.面积图 |
QAbstractSeries::SeriesTypeBar | 2 | A vertical bar chart.垂直条形图。 |
QAbstractSeries::SeriesTypeStackedBar | 3 | A vertical stacked bar chart.垂直堆叠条形图。 |
QAbstractSeries::SeriesTypePercentBar | 4 | A vertical percent bar chart.垂直百分比条形图。 |
QAbstractSeries::SeriesTypePie | 5 | A pie chart.饼图。 |
QAbstractSeries::SeriesTypeScatter | 6 | A scatter chart.散点图。 |
QAbstractSeries::SeriesTypeSpline | 7 | A spline chart.样条曲线图。 |
QAbstractSeries::SeriesTypeHorizontalBar | 8 | A horizontal bar chart.水平条形图。 |
QAbstractSeries::SeriesTypeHorizontalStackedBar | 9 | A horizontal stacked bar chart.水平堆叠条形图。 |
QAbstractSeries::SeriesTypeHorizontalPercentBar | 10 | A horizontal percent bar chart.水平百分比条形图。 |
QAbstractSeries::SeriesTypeBoxPlot | 11 | A box plot chart.箱形图。 |
QAbstractSeries::SeriesTypeCandlestick | 12 | A candlestick chart.烛台图 |
Properties
|
|
Public Functions
virtual | ~QAbstractSeries() |
bool | attachAxis(QAbstractAxis *axis) 将轴指定的轴附着到系列 |
QList<QAbstractAxis *> | attachedAxes() |
QChart * | chart() const 将轴指定的轴附着到系列 |
bool | detachAxis(QAbstractAxis *axis) 从系列中拆离由axis指定的轴。 |
void | hide()将系列的可见性设置为false 。 |
bool | isVisible() const |
QString | name() const |
qreal | opacity() const |
void | setName(const QString &name) |
void | setOpacity(qreal opacity) |
void | setUseOpenGL(bool enable = true) |
void | setVisible(bool visible = true) |
void | show() 将系列的可见性设置为 |
virtual QAbstractSeries::SeriesType | type() const = 0 |
bool | useOpenGL() const |
Signals信号
void | nameChanged() 系列名称更改时发出此信号。 |
void | opacityChanged() 该信号在系列的不透明度改变时发出。 |
void | useOpenGLChanged() 当启用或禁用使用OpenGL加速绘制系列时,将发出此信号。 |
void | visibleChanged()当系列可见性改变时,发出该信号。 |
QAbstractAxis
QAbstractAxis类是用于专门轴类的基类。
Header: | #include <QAbstractAxis> |
Instantiated By: | AbstractAxis |
Inherits: | QObject |
Inherited By: | QBarCategoryAxis, QDateTimeAxis, QLogValueAxis, and QValueAxis |
Properties
- base : qreal 此属性保存对数的底。
- labelFormat : QString 此属性保存轴的标签格式。
- max : qreal 此属性保存轴上的最大值。
- min : qreal 此属性保存轴上的最小值
- minorTickCount : int 此属性保存轴上次要刻度线的数量。这指示在图表上的主刻度之间绘制了多少条网格线。不为次要记号绘制标签。默认值为0。将该值设置为-1,将自动计算主刻度之间的网格线数
- tickCount : const int 此属性保存轴上刻度线的数量。这指示在图表上绘制了多少条网格线。此值为只读。
Public Functions
QLogValueAxis(QObject *parent = nullptr) | |
virtual | ~QLogValueAxis() |
qreal | base() const 此属性保存对数的底。 |
QString | labelFormat() const 此属性保存轴的标签格式。 |
qreal | max() const 此属性保存轴上的最大值 |
qreal | min() const 此属性保存轴上的最小值。 |
int | minorTickCount() const 此属性保存轴上次要刻度线的数量。这指示在图表上的主刻度之间绘制了多少条网格线。不为次要记号绘制标签。默认值为0。将该值设置为-1,将自动计算主刻度之间的网格线数。 |
void | setBase(qreal base) |
void | setLabelFormat(const QString &format) |
void | setMax(qreal max) |
void | setMin(qreal min) |
void | setMinorTickCount(int minorTickCount) |
void | setRange(qreal min, qreal max) |
int | tickCount() const此属性保存轴上刻度线的数量。这指示在图表上绘制了多少条网格线。此值为只读。 |
主题设置例子
themewidget.h
#ifndef THEMEWIDGET_H
#define THEMEWIDGET_H#include <QtWidgets/QWidget>
#include <QtCharts/QChartGlobal>QT_BEGIN_NAMESPACE
class QComboBox;
class QCheckBox;
class Ui_ThemeWidgetForm;
QT_END_NAMESPACEQT_CHARTS_BEGIN_NAMESPACE
class QChartView;
class QChart;
QT_CHARTS_END_NAMESPACEtypedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;QT_CHARTS_USE_NAMESPACEclass ThemeWidget: public QWidget
{Q_OBJECT
public:explicit ThemeWidget(QWidget *parent = 0);~ThemeWidget();private Q_SLOTS:void updateUI();private:DataTable generateRandomData(int listCount, int valueMax, int valueCount) const;void populateThemeBox();void populateAnimationBox();void populateLegendBox();void connectSignals();QChart *createAreaChart() const;QChart *createBarChart(int valueCount) const;QChart *createPieChart() const;QChart *createLineChart() const;QChart *createSplineChart() const;QChart *createScatterChart() const;private:int m_listCount;int m_valueMax;int m_valueCount;QList<QChartView *> m_charts;DataTable m_dataTable;Ui_ThemeWidgetForm *m_ui;
};#endif /* THEMEWIDGET_H */
themewidget.cpp
#include "themewidget.h"
#include "ui_themewidget.h"#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCharts/QAbstractBarSeries>
#include <QtCharts/QPercentBarSeries>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QLegend>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QBarCategoryAxis>
#include <QtWidgets/QApplication>
#include <QtCharts/QValueAxis>
//对应的构造函数
ThemeWidget::ThemeWidget(QWidget *parent) :QWidget(parent),m_listCount(3),m_valueMax(10),m_valueCount(7),m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),m_ui(new Ui_ThemeWidgetForm)
{m_ui->setupUi(this);populateThemeBox();populateAnimationBox();populateLegendBox();//create chartsQChartView *chartView;chartView = new QChartView(createAreaChart());
// 设置对应的布局m_ui->gridLayout->addWidget(chartView, 1, 0);m_charts << chartView;chartView = new QChartView(createPieChart());// Funny things happen if the pie slice labels do not fit the screen, so we ignore size policychartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);m_ui->gridLayout->addWidget(chartView, 1, 1);m_charts << chartView;//![5]chartView = new QChartView(createLineChart());m_ui->gridLayout->addWidget(chartView, 1, 2);//![5]m_charts << chartView;chartView = new QChartView(createBarChart(m_valueCount));m_ui->gridLayout->addWidget(chartView, 2, 0);m_charts << chartView;chartView = new QChartView(createSplineChart());m_ui->gridLayout->addWidget(chartView, 2, 1);m_charts << chartView;chartView = new QChartView(createScatterChart());m_ui->gridLayout->addWidget(chartView, 2, 2);m_charts << chartView;// Set defaultsm_ui->antialiasCheckBox->setChecked(true);// Set the colors from the light theme as default onesQPalette pal = qApp->palette();pal.setColor(QPalette::Window, QRgb(0xf0f0f0));pal.setColor(QPalette::WindowText, QRgb(0x404044));qApp->setPalette(pal);updateUI();
}ThemeWidget::~ThemeWidget()
{delete m_ui;
}DataTable ThemeWidget::generateRandomData(int listCount, int valueMax, int valueCount) const
{DataTable dataTable;// generate random datafor (int i(0); i < listCount; i++) {DataList dataList;qreal yValue(0);for (int j(0); j < valueCount; j++) {yValue = yValue + QRandomGenerator::global()->bounded(valueMax / (qreal) valueCount);QPointF value((j + QRandomGenerator::global()->generateDouble()) * ((qreal) m_valueMax / (qreal) valueCount),yValue);QString label = "Slice " + QString::number(i) + ":" + QString::number(j);dataList << Data(value, label);}dataTable << dataList;}return dataTable;
}
// 设置视图的主题
void ThemeWidget::populateThemeBox()
{// add items to theme comboboxm_ui->themeComboBox->addItem("Light", QChart::ChartThemeLight);m_ui->themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);m_ui->themeComboBox->addItem("Dark", QChart::ChartThemeDark);m_ui->themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);m_ui->themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);m_ui->themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);m_ui->themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);m_ui->themeComboBox->addItem("Qt", QChart::ChartThemeQt);
}
//设置对应背景
void ThemeWidget::populateAnimationBox()
{// add items to animation comboboxm_ui->animatedComboBox->addItem("No Animations", QChart::NoAnimation);m_ui->animatedComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);m_ui->animatedComboBox->addItem("Series Animations", QChart::SeriesAnimations);m_ui->animatedComboBox->addItem("All Animations", QChart::AllAnimations);
}
//设置对应的标签的位置
void ThemeWidget::populateLegendBox()
{// add items to legend comboboxm_ui->legendComboBox->addItem("No Legend ", 0);m_ui->legendComboBox->addItem("Legend Top", Qt::AlignTop);m_ui->legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);m_ui->legendComboBox->addItem("Legend Left", Qt::AlignLeft);m_ui->legendComboBox->addItem("Legend Right", Qt::AlignRight);
}QChart *ThemeWidget::createAreaChart() const
{QChart *chart = new QChart();chart->setTitle("Area chart");// The lower series initialized to zero valuesQLineSeries *lowerSeries = 0;QString name("Series ");int nameIndex = 0;for (int i(0); i < m_dataTable.count(); i++) {QLineSeries *upperSeries = new QLineSeries(chart);for (int j(0); j < m_dataTable[i].count(); j++) {Data data = m_dataTable[i].at(j);if (lowerSeries) {const QVector<QPointF>& points = lowerSeries->pointsVector();upperSeries->append(QPointF(j, points[i].y() + data.first.y()));} else {upperSeries->append(QPointF(j, data.first.y()));}}QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);area->setName(name + QString::number(nameIndex));nameIndex++;chart->addSeries(area);lowerSeries = upperSeries;}chart->createDefaultAxes();chart->axes(Qt::Horizontal).first()->setRange(0, m_valueCount - 1);chart->axes(Qt::Vertical).first()->setRange(0, m_valueMax);// Add space to label to add space between labels and axisQValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());Q_ASSERT(axisY);axisY->setLabelFormat("%.1f ");return chart;
}QChart *ThemeWidget::createBarChart(int valueCount) const
{Q_UNUSED(valueCount);QChart *chart = new QChart();
// 设置对应的标签chart->setTitle("Bar chart");QStackedBarSeries *series = new QStackedBarSeries(chart);for (int i(0); i < m_dataTable.count(); i++) {QBarSet *set = new QBarSet("Bar set " + QString::number(i));for (const Data &data : m_dataTable[i])*set << data.first.y();series->append(set);}chart->addSeries(series);chart->createDefaultAxes();chart->axes(Qt::Vertical).first()->setRange(0, m_valueMax * 2);// Add space to label to add space between labels and axisQValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());Q_ASSERT(axisY);axisY->setLabelFormat("%.1f ");return chart;
}QChart *ThemeWidget::createLineChart() const
{//![1]QChart *chart = new QChart();chart->setTitle("Line chart");//![1]//![2]QString name("Series ");int nameIndex = 0;for (const DataList &list : m_dataTable) {QLineSeries *series = new QLineSeries(chart);for (const Data &data : list)series->append(data.first);series->setName(name + QString::number(nameIndex));nameIndex++;chart->addSeries(series);}//![2]//![3]chart->createDefaultAxes();chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);//![3]//![4]// Add space to label to add space between labels and axisQValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());Q_ASSERT(axisY);axisY->setLabelFormat("%.1f ");//![4]return chart;
}QChart *ThemeWidget::createPieChart() const
{QChart *chart = new QChart();chart->setTitle("Pie chart");QPieSeries *series = new QPieSeries(chart);for (const Data &data : m_dataTable[0]) {QPieSlice *slice = series->append(data.second, data.first.y());if (data == m_dataTable[0].first()) {// Show the first slice exploded with labelslice->setLabelVisible();slice->setExploded();slice->setExplodeDistanceFactor(0.5);}}series->setPieSize(0.4);chart->addSeries(series);return chart;
}QChart *ThemeWidget::createSplineChart() const
{QChart *chart = new QChart();chart->setTitle("Spline chart");QString name("Series ");int nameIndex = 0;for (const DataList &list : m_dataTable) {QSplineSeries *series = new QSplineSeries(chart);for (const Data &data : list)series->append(data.first);series->setName(name + QString::number(nameIndex));nameIndex++;chart->addSeries(series);}chart->createDefaultAxes();chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);// Add space to label to add space between labels and axisQValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());Q_ASSERT(axisY);axisY->setLabelFormat("%.1f ");return chart;
}QChart *ThemeWidget::createScatterChart() const
{// scatter chartQChart *chart = new QChart();chart->setTitle("Scatter chart");QString name("Series ");int nameIndex = 0;for (const DataList &list : m_dataTable) {QScatterSeries *series = new QScatterSeries(chart);for (const Data &data : list)series->append(data.first);series->setName(name + QString::number(nameIndex));nameIndex++;chart->addSeries(series);}chart->createDefaultAxes();chart->axes(Qt::Horizontal).first()->setRange(0, m_valueMax);chart->axes(Qt::Vertical).first()->setRange(0, m_valueCount);// Add space to label to add space between labels and axisQValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());Q_ASSERT(axisY);axisY->setLabelFormat("%.1f ");return chart;
}void ThemeWidget::updateUI()
{//![6]QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(m_ui->themeComboBox->itemData(m_ui->themeComboBox->currentIndex()).toInt());//![6]const auto charts = m_charts;if (!m_charts.isEmpty() && m_charts.at(0)->chart()->theme() != theme) {for (QChartView *chartView : charts) {//![7]chartView->chart()->setTheme(theme);//![7]}// Set palette colors based on selected theme//![8]QPalette pal = window()->palette();if (theme == QChart::ChartThemeLight) {pal.setColor(QPalette::Window, QRgb(0xf0f0f0));pal.setColor(QPalette::WindowText, QRgb(0x404044));//![8]} else if (theme == QChart::ChartThemeDark) {pal.setColor(QPalette::Window, QRgb(0x121218));pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));} else if (theme == QChart::ChartThemeBlueCerulean) {pal.setColor(QPalette::Window, QRgb(0x40434a));pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));} else if (theme == QChart::ChartThemeBrownSand) {pal.setColor(QPalette::Window, QRgb(0x9e8965));pal.setColor(QPalette::WindowText, QRgb(0x404044));} else if (theme == QChart::ChartThemeBlueNcs) {pal.setColor(QPalette::Window, QRgb(0x018bba));pal.setColor(QPalette::WindowText, QRgb(0x404044));} else if (theme == QChart::ChartThemeHighContrast) {pal.setColor(QPalette::Window, QRgb(0xffab03));pal.setColor(QPalette::WindowText, QRgb(0x181818));} else if (theme == QChart::ChartThemeBlueIcy) {pal.setColor(QPalette::Window, QRgb(0xcee7f0));pal.setColor(QPalette::WindowText, QRgb(0x404044));} else {pal.setColor(QPalette::Window, QRgb(0xf0f0f0));pal.setColor(QPalette::WindowText, QRgb(0x404044));}window()->setPalette(pal);}// Update antialiasing//![11]bool checked = m_ui->antialiasCheckBox->isChecked();for (QChartView *chart : charts)chart->setRenderHint(QPainter::Antialiasing, checked);//![11]// Update animation options//![9]QChart::AnimationOptions options(m_ui->animatedComboBox->itemData(m_ui->animatedComboBox->currentIndex()).toInt());if (!m_charts.isEmpty() && m_charts.at(0)->chart()->animationOptions() != options) {for (QChartView *chartView : charts)chartView->chart()->setAnimationOptions(options);}//![9]// Update legend alignment//![10]Qt::Alignment alignment(m_ui->legendComboBox->itemData(m_ui->legendComboBox->currentIndex()).toInt());if (!alignment) {for (QChartView *chartView : charts)chartView->chart()->legend()->hide();} else {for (QChartView *chartView : charts) {chartView->chart()->legend()->setAlignment(alignment);chartView->chart()->legend()->show();}}//![10]
}
main.cpp
#include "themewidget.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>int main(int argc, char *argv[])
{QApplication a(argc, argv);QMainWindow window;ThemeWidget *widget = new ThemeWidget();window.setCentralWidget(widget);window.resize(900, 600);window.show();return a.exec();
}
结果: