• Qt Creator实例之图标主题


    Chart themes 是 Qt Creator 中图表的主题,它可以用于改变图表的外观和风格,使其更符合你的需求和设计。此示例显示了所有支持的图表类型的不同内置主题的外观。为了给结果一个更和谐的外观,应用程序的背景调色板是根据所选主题定制的。

    chartthemes/chartthemes.pro

    1. QT += charts
    2. HEADERS += \
    3.     themewidget.h
    4. SOURCES += \
    5.     main.cpp \
    6.     themewidget.cpp
    7. target.path = $$[QT_INSTALL_EXAMPLES]/charts/chartthemes
    8. INSTALLS += target

    chartthemes/chartthemes.h

    1. #ifndef THEMEWIDGET_H
    2. #define THEMEWIDGET_H
    3. #include 
    4. #include 
    5. QT_BEGIN_NAMESPACE
    6. class QComboBox;
    7. class QCheckBox;
    8. QT_END_NAMESPACE
    9. QT_CHARTS_BEGIN_NAMESPACE
    10. class QChartView;
    11. class QChart;
    12. QT_CHARTS_END_NAMESPACE
    13. typedef QPair Data;
    14. typedef QList DataList;
    15. typedef QList DataTable;
    16. QT_CHARTS_USE_NAMESPACE
    17. class ThemeWidgetpublic QWidget
    18. {
    19.     Q_OBJECT
    20. public:
    21.     explicit ThemeWidget(QWidget *parent = 0);
    22.     ~ThemeWidget();
    23. private Q_SLOTS:
    24.     void updateUI();
    25. private:
    26.     DataTable generateRandomData(int listCount, int valueMax, int valueCount) const;
    27.     QComboBox *createThemeBox() const;
    28.     QComboBox *createAnimationBox() const;
    29.     QComboBox *createLegendBox() const;
    30.     void connectSignals();
    31.     QChart *createAreaChart() const;
    32.     QChart *createBarChart(int valueCount) const;
    33.     QChart *createPieChart() const;
    34.     QChart *createLineChart() const;
    35.     QChart *createSplineChart() const;
    36.     QChart *createScatterChart() const;
    37. private:
    38.     int m_listCount;
    39.     int m_valueMax;
    40.     int m_valueCount;
    41.     QList m_charts;
    42.     DataTable m_dataTable;
    43.     QComboBox *m_themeComboBox;
    44.     QCheckBox *m_antialiasCheckBox;
    45.     QComboBox *m_animatedComboBox;
    46.     QComboBox *m_legendComboBox;
    47. };
    48. #endif /* THEMEWIDGET_H */

    chartthemes/main.cpp

    1. #include "themewidget.h"
    2. #include 
    3. #include 
    4. int main(int argc, char *argv[])
    5. {
    6.     QApplication a(argc, argv);
    7.     QMainWindow window;
    8.     ThemeWidget *widget = new ThemeWidget();
    9.     window.setCentralWidget(widget);
    10.     window.resize(900600);
    11.     window.show();
    12.     return a.exec();
    13. }

    chartthemes/chartthemes.cpp

    1. /****************************************************************************
    2. **
    3. ** Copyright (C) 2016 The Qt Company Ltd.
    4. ** Contact: https://www.qt.io/licensing/
    5. **
    6. ** This file is part of the Qt Charts module of the Qt Toolkit.
    7. **
    8. ** $QT_BEGIN_LICENSE:GPL$
    9. ** Commercial License Usage
    10. ** Licensees holding valid commercial Qt licenses may use this file in
    11. ** accordance with the commercial license agreement provided with the
    12. ** Software or, alternatively, in accordance with the terms contained in
    13. ** a written agreement between you and The Qt Company. For licensing terms
    14. ** and conditions see https://www.qt.io/terms-conditions. For further
    15. ** information use the contact form at https://www.qt.io/contact-us.
    16. **
    17. ** GNU General Public License Usage
    18. ** Alternatively, this file may be used under the terms of the GNU
    19. ** General Public License version 3 or (at your option) any later version
    20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
    21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
    22. ** included in the packaging of this file. Please review the following
    23. ** information to ensure the GNU General Public License requirements will
    24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
    25. **
    26. ** $QT_END_LICENSE$
    27. **
    28. ****************************************************************************/
    29. #include "themewidget.h"
    30. #include <QtCharts/QChartView>
    31. #include <QtCharts/QPieSeries>
    32. #include <QtCharts/QPieSlice>
    33. #include <QtCharts/QAbstractBarSeries>
    34. #include <QtCharts/QPercentBarSeries>
    35. #include <QtCharts/QStackedBarSeries>
    36. #include <QtCharts/QBarSeries>
    37. #include <QtCharts/QBarSet>
    38. #include <QtCharts/QLineSeries>
    39. #include <QtCharts/QSplineSeries>
    40. #include <QtCharts/QScatterSeries>
    41. #include <QtCharts/QAreaSeries>
    42. #include <QtCharts/QLegend>
    43. #include <QtWidgets/QGridLayout>
    44. #include <QtWidgets/QFormLayout>
    45. #include <QtWidgets/QComboBox>
    46. #include <QtWidgets/QSpinBox>
    47. #include <QtWidgets/QCheckBox>
    48. #include <QtWidgets/QGroupBox>
    49. #include <QtWidgets/QLabel>
    50. #include <QtCore/QTime>
    51. #include <QtCharts/QBarCategoryAxis>
    52. ThemeWidget::ThemeWidget(QWidget *parent) :
    53.     QWidget(parent),
    54.     m_listCount(3),
    55.     m_valueMax(10),
    56.     m_valueCount(7),
    57.     m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
    58.     m_themeComboBox(createThemeBox()),
    59.     m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
    60.     m_animatedComboBox(createAnimationBox()),
    61.     m_legendComboBox(createLegendBox())
    62. {
    63.     connectSignals();
    64.     // create layout
    65.     QGridLayout *baseLayout = new QGridLayout();
    66.     QHBoxLayout *settingsLayout = new QHBoxLayout();
    67.     settingsLayout->addWidget(new QLabel("Theme:"));
    68.     settingsLayout->addWidget(m_themeComboBox);
    69.     settingsLayout->addWidget(new QLabel("Animation:"));
    70.     settingsLayout->addWidget(m_animatedComboBox);
    71.     settingsLayout->addWidget(new QLabel("Legend:"));
    72.     settingsLayout->addWidget(m_legendComboBox);
    73.     settingsLayout->addWidget(m_antialiasCheckBox);
    74.     settingsLayout->addStretch();
    75.     baseLayout->addLayout(settingsLayout, 0013);
    76.     //create charts
    77.     QChartView *chartView;
    78.     chartView = new QChartView(createAreaChart());
    79.     baseLayout->addWidget(chartView, 10);
    80.     m_charts << chartView;
    81.     chartView = new QChartView(createBarChart(m_valueCount));
    82.     baseLayout->addWidget(chartView, 11);
    83.     m_charts << chartView;
    84.     chartView = new QChartView(createLineChart());
    85.     baseLayout->addWidget(chartView, 12);
    86.     m_charts << chartView;
    87.     chartView = new QChartView(createPieChart());
    88.     // Funny things happen if the pie slice labels do not fit the screen, so we ignore size policy
    89.     chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    90.     baseLayout->addWidget(chartView, 20);
    91.     m_charts << chartView;
    92.     chartView = new QChartView(createSplineChart());
    93.     baseLayout->addWidget(chartView, 21);
    94.     m_charts << chartView;
    95.     chartView =
    96.             
    97.             new QChartView(createScatterChart());
    98.     baseLayout->addWidget(chartView, 22);
    99.     m_charts << chartView;
    100.     setLayout(baseLayout);
    101.     // Set defaults
    102.     m_antialiasCheckBox->setChecked(true);
    103.     updateUI();
    104. }
    105. ThemeWidget::~ThemeWidget()
    106. {
    107. }
    108. void ThemeWidget::connectSignals()
    109. {
    110.     connect(m_themeComboBox,
    111.             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
    112.             this, &ThemeWidget::updateUI);
    113.     connect(m_antialiasCheckBox, &QCheckBox::toggled, this, &ThemeWidget::updateUI);
    114.     connect(m_animatedComboBox,
    115.             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
    116.             this, &ThemeWidget::updateUI);
    117.     connect(m_legendComboBox,
    118.             static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
    119.             this, &ThemeWidget::updateUI);
    120. }
    121. DataTable ThemeWidget::generateRandomData(int listCount, int valueMax, int valueCount) const
    122. {
    123.     DataTable dataTable;
    124.     // set seed for random stuff
    125.     qsrand(QTime(000).secsTo(QTime::currentTime()));
    126.     // generate random data
    127.     for (int i(0); i < listCount; i++) {
    128.         DataList dataList;
    129.         qreal yValue(0);
    130.         for (int j(0); j < valueCount; j++) {
    131.             yValue = yValue + (qreal)(qrand() % valueMax) / (qreal) valueCount;
    132.             QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
    133.                           yValue);
    134.             QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
    135.             dataList << Data(value, label);
    136.         }
    137.         dataTable << dataList;
    138.     }
    139.     return dataTable;
    140. }
    141. QComboBox *ThemeWidget::createThemeBox() const
    142. {
    143.     // settings layout
    144.     QComboBox *themeComboBox = new QComboBox();
    145.     themeComboBox->addItem("Light", QChart::ChartThemeLight);
    146.     themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
    147.     themeComboBox->addItem("Dark", QChart::ChartThemeDark);
    148.     themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
    149.     themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
    150.     themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
    151.     themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
    152.     themeComboBox->addItem("Qt", QChart::ChartThemeQt);
    153.     return themeComboBox;
    154. }
    155. QComboBox *ThemeWidget::createAnimationBox() const
    156. {
    157.     // settings layout
    158.     QComboBox *animationComboBox = new QComboBox();
    159.     animationComboBox->addItem("No Animations", QChart::NoAnimation);
    160.     animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
    161.     animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
    162.     animationComboBox->addItem("All Animations", QChart::AllAnimations);
    163.     return animationComboBox;
    164. }
    165. QComboBox *ThemeWidget::createLegendBox() const
    166. {
    167.     QComboBox *legendComboBox = new QComboBox();
    168.     legendComboBox->addItem("No Legend "0);
    169.     legendComboBox->addItem("Legend Top", Qt::AlignTop);
    170.     legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
    171.     legendComboBox->addItem("Legend Left", Qt::AlignLeft);
    172.     legendComboBox->addItem("Legend Right", Qt::AlignRight);
    173.     return legendComboBox;
    174. }
    175. QChart *ThemeWidget::createAreaChart() const
    176. {
    177.     QChart *chart = new QChart();
    178.     chart->setTitle("Area chart");
    179.     // The lower series initialized to zero values
    180.     QLineSeries *lowerSeries = 0;
    181.     QString name("Series ");
    182.     int nameIndex = 0;
    183.     for (int i(0); i < m_dataTable.count(); i++) {
    184.         QLineSeries *upperSeries = new QLineSeries(chart);
    185.         for (int j(0); j < m_dataTable[i].count(); j++) {
    186.             Data data = m_dataTable[i].at(j);
    187.             if (lowerSeries) {
    188.                 const QVector<QPointF>& points = lowerSeries->pointsVector();
    189.                 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
    190.             } else {
    191.                 upperSeries->append(QPointF(j, data.first.y()));
    192.             }
    193.         }
    194.         QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
    195.         area->setName(name + QString::number(nameIndex));
    196.         nameIndex++;
    197.         chart->addSeries(area);
    198.         chart->createDefaultAxes();
    199.         lowerSeries = upperSeries;
    200.     }
    201.     return chart;
    202. }
    203. QChart *ThemeWidget::createBarChart(int valueCount) const
    204. {
    205.     Q_UNUSED(valueCount);
    206.     QChart *chart = new QChart();
    207.     chart->setTitle("Bar chart");
    208.     QStackedBarSeries *series = new QStackedBarSeries(chart);
    209.     for (int i(0); i < m_dataTable.count(); i++) {
    210.         QBarSet *set = new QBarSet("Bar set " + QString::number(i));
    211.         for (const Data &data : m_dataTable[i])
    212.             *set << data.first.y();
    213.         series->append(set);
    214.     }
    215.     chart->addSeries(series);
    216.     chart->createDefaultAxes();
    217.     return chart;
    218. }
    219. QChart *ThemeWidget::createLineChart() const
    220. {
    221.     QChart *chart = new QChart();
    222.     chart->setTitle("Line chart");
    223.     QString name("Series ");
    224.     int nameIndex = 0;
    225.     for (const DataList &list : m_dataTable) {
    226.         QLineSeries *series = new QLineSeries(chart);
    227.         for (const Data &data : list)
    228.             series->append(data.first);
    229.         series->setName(name + QString::number(nameIndex));
    230.         nameIndex++;
    231.         chart->addSeries(series);
    232.     }
    233.     chart->createDefaultAxes();
    234.     return chart;
    235. }
    236. QChart *ThemeWidget::createPieChart() const
    237. {
    238.     QChart *chart = new QChart();
    239.     chart->setTitle("Pie chart");
    240.     qreal pieSize = 1.0 / m_dataTable.count();
    241.     for (int i = 0; i < m_dataTable.count(); i++) {
    242.         QPieSeries *series = new QPieSeries(chart);
    243.         for (const Data &data : m_dataTable[i]) {
    244.             QPieSlice *slice = series->append(data.second, data.first.y());
    245.             if (data == m_dataTable[i].first()) {
    246.                 slice->setLabelVisible();
    247.                 slice->setExploded();
    248.             }
    249.         }
    250.         qreal hPos = (pieSize / 2+ (i / (qreal) m_dataTable.count());
    251.         series->setPieSize(pieSize);
    252.         series->setHorizontalPosition(hPos);
    253.         series->setVerticalPosition(0.5);
    254.         chart->addSeries(series);
    255.     }
    256.     return chart;
    257. }
    258. QChart *ThemeWidget::createSplineChart() const
    259. {
    260.     // spine chart
    261.     QChart *chart = new QChart();
    262.     chart->setTitle("Spline chart");
    263.     QString name("Series ");
    264.     int nameIndex = 0;
    265.     for (const DataList &list : m_dataTable) {
    266.         QSplineSeries *series = new QSplineSeries(chart);
    267.         for (const Data &data : list)
    268.             series->append(data.first);
    269.         series->setName(name + QString::number(nameIndex));
    270.         nameIndex++;
    271.         chart->addSeries(series);
    272.     }
    273.     chart->createDefaultAxes();
    274.     return chart;
    275. }
    276. QChart *ThemeWidget::createScatterChart() const
    277. {
    278.     // scatter chart
    279.     QChart *chart = new QChart();
    280.     chart->setTitle("Scatter chart");
    281.     QString name("Series ");
    282.     int nameIndex = 0;
    283.     for (const DataList &list : m_dataTable) {
    284.         QScatterSeries *series = new QScatterSeries(chart);
    285.         for (const Data &data : list)
    286.             series->append(data.first);
    287.         series->setName(name + QString::number(nameIndex));
    288.         nameIndex++;
    289.         chart->addSeries(series);
    290.     }
    291.     chart->createDefaultAxes();
    292.     return chart;
    293. }
    294. void ThemeWidget::updateUI()
    295. {
    296.     QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(
    297.                 m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt());
    298.     const auto charts = m_charts;
    299.     if (m_charts.at(0)->chart()->theme() != theme) {
    300.         for (QChartView *chartView : charts)
    301.             chartView->chart()->setTheme(theme);
    302.         QPalette pal = window()->palette();
    303.         if (theme == QChart::ChartThemeLight) {
    304.             pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
    305.             pal.setColor(QPalette::WindowText, QRgb(0x404044));
    306.         } else if (theme == QChart::ChartThemeDark) {
    307.             pal.setColor(QPalette::Window, QRgb(0x121218));
    308.             pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
    309.         } else if (theme == QChart::ChartThemeBlueCerulean) {
    310.             pal.setColor(QPalette::Window, QRgb(0x40434a));
    311.             pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
    312.         } else if (theme == QChart::ChartThemeBrownSand) {
    313.             pal.setColor(QPalette::Window, QRgb(0x9e8965));
    314.             pal.setColor(QPalette::WindowText, QRgb(0x404044));
    315.         } else if (theme == QChart::ChartThemeBlueNcs) {
    316.             pal.setColor(QPalette::Window, QRgb(0x018bba));
    317.             pal.setColor(QPalette::WindowText, QRgb(0x404044));
    318.         } else if (theme == QChart::ChartThemeHighContrast) {
    319.             pal.setColor(QPalette::Window, QRgb(0xffab03));
    320.             pal.setColor(QPalette::WindowText, QRgb(0x181818));
    321.         } else if (theme == QChart::ChartThemeBlueIcy) {
    322.             pal.setColor(QPalette::Window, QRgb(0xcee7f0));
    323.             pal.setColor(QPalette::WindowText, QRgb(0x404044));
    324.         } else {
    325.             pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
    326.             pal.setColor(QPalette::WindowText, QRgb(0x404044));
    327.         }
    328.         window()->setPalette(pal);
    329.     }
    330.     bool checked = m_antialiasCheckBox->isChecked();
    331.     for (QChartView *chart : charts)
    332.         chart->setRenderHint(QPainter::Antialiasing, checked);
    333.     QChart::AnimationOptions options(
    334.                 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
    335.     if (m_charts.at(0)->chart()->animationOptions() != options) {
    336.         for (QChartView *chartView : charts)
    337.             chartView->chart()->setAnimationOptions(options);
    338.     }
    339.     Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
    340.     if (!alignment) {
    341.         for (QChartView *chartView : charts)
    342.             chartView->chart()->legend()->hide();
    343.     } else {
    344.         for (QChartView *chartView : charts) {
    345.             chartView->chart()->legend()->setAlignment(alignment);
    346.             chartView->chart()->legend()->show();
    347.         }
    348.     }
    349. }
  • 相关阅读:
    ubuntu http 服务器响应
    Jamba: A Hybrid Transformer-Mamba Language Model
    RAD Studio 11.2详解其务实改进(Delphi & C++ Builder)-Alexandria
    python之图形用户界面,如何安装wxPython
    图数据库|基于 Nebula Graph 的 BetweennessCentrality 算法
    Jenkins建项发版测试
    火遍国内外IT技术圈,豆瓣 9.7!这本技术书籍直接封神了
    【机器人算法】机器人运动学参数辨识/DH参数校准/DH参数辨识
    新版TCGA的甲基化数据分析
    软件开发全生命周期必备文档整理(@附文档下载)
  • 原文地址:https://blog.csdn.net/weixin_41055260/article/details/137396064