diff options
author | Titta Heikkala <titta.heikkala@qt.io> | 2017-09-01 13:21:51 +0300 |
---|---|---|
committer | Titta Heikkala <titta.heikkala@qt.io> | 2017-09-04 07:15:06 +0000 |
commit | 5082ca305b14be3dde25862d0ce4fd32d688d985 (patch) | |
tree | 614508ac5f92eb466e446b7ed4708ea0a6e717ae | |
parent | f64bd90920b80be63c318fed7613c87ff70cce1b (diff) |
Add Qt Designer UI file to Chart Themes Example
Add a UI file to Chart Themes Example. The UI file includes widgets
to change chart options along with signal slot connections.
Change-Id: I194624e23ebb785271a054cf1b2891d134fd2656
Task-number: QTBUG-60662
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
-rw-r--r-- | examples/charts/chartthemes/chartthemes.pro | 3 | ||||
-rw-r--r-- | examples/charts/chartthemes/themewidget.cpp | 118 | ||||
-rw-r--r-- | examples/charts/chartthemes/themewidget.h | 12 | ||||
-rw-r--r-- | examples/charts/chartthemes/themewidget.ui | 103 |
4 files changed, 155 insertions, 81 deletions
diff --git a/examples/charts/chartthemes/chartthemes.pro b/examples/charts/chartthemes/chartthemes.pro index 5985f55c..193dc4e6 100644 --- a/examples/charts/chartthemes/chartthemes.pro +++ b/examples/charts/chartthemes/chartthemes.pro @@ -9,3 +9,6 @@ SOURCES += \ target.path = $$[QT_INSTALL_EXAMPLES]/charts/chartthemes INSTALLS += target + +FORMS += \ + themewidget.ui diff --git a/examples/charts/chartthemes/themewidget.cpp b/examples/charts/chartthemes/themewidget.cpp index bcdd2e7f..f8f550d2 100644 --- a/examples/charts/chartthemes/themewidget.cpp +++ b/examples/charts/chartthemes/themewidget.cpp @@ -28,6 +28,7 @@ ****************************************************************************/ #include "themewidget.h" +#include "ui_themewidget.h" #include <QtCharts/QChartView> #include <QtCharts/QPieSeries> @@ -59,59 +60,45 @@ ThemeWidget::ThemeWidget(QWidget *parent) : m_valueMax(10), m_valueCount(7), m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)), - m_themeComboBox(createThemeBox()), - m_antialiasCheckBox(new QCheckBox("Anti-aliasing")), - m_animatedComboBox(createAnimationBox()), - m_legendComboBox(createLegendBox()) + m_ui(new Ui_ThemeWidgetForm) { - connectSignals(); - // create layout - QGridLayout *baseLayout = new QGridLayout(); - QHBoxLayout *settingsLayout = new QHBoxLayout(); - settingsLayout->addWidget(new QLabel("Theme:")); - settingsLayout->addWidget(m_themeComboBox); - settingsLayout->addWidget(new QLabel("Animation:")); - settingsLayout->addWidget(m_animatedComboBox); - settingsLayout->addWidget(new QLabel("Legend:")); - settingsLayout->addWidget(m_legendComboBox); - settingsLayout->addWidget(m_antialiasCheckBox); - settingsLayout->addStretch(); - baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); + m_ui->setupUi(this); + populateThemeBox(); + populateAnimationBox(); + populateLegendBox(); //create charts QChartView *chartView; chartView = new QChartView(createAreaChart()); - baseLayout->addWidget(chartView, 1, 0); + m_ui->gridLayout->addWidget(chartView, 1, 0); m_charts << chartView; chartView = new QChartView(createBarChart(m_valueCount)); - baseLayout->addWidget(chartView, 1, 1); + m_ui->gridLayout->addWidget(chartView, 1, 1); m_charts << chartView; chartView = new QChartView(createLineChart()); - baseLayout->addWidget(chartView, 1, 2); + m_ui->gridLayout->addWidget(chartView, 1, 2); m_charts << chartView; chartView = new QChartView(createPieChart()); // Funny things happen if the pie slice labels do not fit the screen, so we ignore size policy chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); - baseLayout->addWidget(chartView, 2, 0); + m_ui->gridLayout->addWidget(chartView, 2, 0); m_charts << chartView; chartView = new QChartView(createSplineChart()); - baseLayout->addWidget(chartView, 2, 1); + m_ui->gridLayout->addWidget(chartView, 2, 1); m_charts << chartView; chartView = new QChartView(createScatterChart()); - baseLayout->addWidget(chartView, 2, 2); + m_ui->gridLayout->addWidget(chartView, 2, 2); m_charts << chartView; - setLayout(baseLayout); - // Set defaults - m_antialiasCheckBox->setChecked(true); + m_ui->antialiasCheckBox->setChecked(true); // Set the colors from the light theme as default ones QPalette pal = qApp->palette(); @@ -124,20 +111,7 @@ ThemeWidget::ThemeWidget(QWidget *parent) : ThemeWidget::~ThemeWidget() { -} - -void ThemeWidget::connectSignals() -{ - connect(m_themeComboBox, - static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), - this, &ThemeWidget::updateUI); - connect(m_antialiasCheckBox, &QCheckBox::toggled, this, &ThemeWidget::updateUI); - connect(m_animatedComboBox, - static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), - this, &ThemeWidget::updateUI); - connect(m_legendComboBox, - static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), - this, &ThemeWidget::updateUI); + delete m_ui; } DataTable ThemeWidget::generateRandomData(int listCount, int valueMax, int valueCount) const @@ -161,41 +135,36 @@ DataTable ThemeWidget::generateRandomData(int listCount, int valueMax, int value return dataTable; } -QComboBox *ThemeWidget::createThemeBox() const +void ThemeWidget::populateThemeBox() { - // settings layout - QComboBox *themeComboBox = new QComboBox(); - themeComboBox->addItem("Light", QChart::ChartThemeLight); - themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); - themeComboBox->addItem("Dark", QChart::ChartThemeDark); - themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); - themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); - themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); - themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); - themeComboBox->addItem("Qt", QChart::ChartThemeQt); - return themeComboBox; + // add items to theme combobox + m_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); } -QComboBox *ThemeWidget::createAnimationBox() const +void ThemeWidget::populateAnimationBox() { - // settings layout - QComboBox *animationComboBox = new QComboBox(); - animationComboBox->addItem("No Animations", QChart::NoAnimation); - animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); - animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); - animationComboBox->addItem("All Animations", QChart::AllAnimations); - return animationComboBox; + // add items to animation combobox + m_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); } -QComboBox *ThemeWidget::createLegendBox() const +void ThemeWidget::populateLegendBox() { - QComboBox *legendComboBox = new QComboBox(); - legendComboBox->addItem("No Legend ", 0); - legendComboBox->addItem("Legend Top", Qt::AlignTop); - legendComboBox->addItem("Legend Bottom", Qt::AlignBottom); - legendComboBox->addItem("Legend Left", Qt::AlignLeft); - legendComboBox->addItem("Legend Right", Qt::AlignRight); - return legendComboBox; + // add items to legend combobox + m_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 @@ -334,10 +303,10 @@ QChart *ThemeWidget::createScatterChart() const void ThemeWidget::updateUI() { QChart::ChartTheme theme = static_cast<QChart::ChartTheme>( - m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt()); + m_ui->themeComboBox->itemData(m_ui->themeComboBox->currentIndex()).toInt()); const auto charts = m_charts; - if (m_charts.at(0)->chart()->theme() != theme) { + if (!m_charts.isEmpty() && m_charts.at(0)->chart()->theme() != theme) { for (QChartView *chartView : charts) chartView->chart()->setTheme(theme); @@ -370,18 +339,19 @@ void ThemeWidget::updateUI() window()->setPalette(pal); } - bool checked = m_antialiasCheckBox->isChecked(); + bool checked = m_ui->antialiasCheckBox->isChecked(); for (QChartView *chart : charts) chart->setRenderHint(QPainter::Antialiasing, checked); QChart::AnimationOptions options( - m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); - if (m_charts.at(0)->chart()->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); } - Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); + Qt::Alignment alignment( + m_ui->legendComboBox->itemData(m_ui->legendComboBox->currentIndex()).toInt()); if (!alignment) { for (QChartView *chartView : charts) diff --git a/examples/charts/chartthemes/themewidget.h b/examples/charts/chartthemes/themewidget.h index 5b31ab7c..e2e093ab 100644 --- a/examples/charts/chartthemes/themewidget.h +++ b/examples/charts/chartthemes/themewidget.h @@ -36,6 +36,7 @@ QT_BEGIN_NAMESPACE class QComboBox; class QCheckBox; +class Ui_ThemeWidgetForm; QT_END_NAMESPACE QT_CHARTS_BEGIN_NAMESPACE @@ -61,9 +62,9 @@ private Q_SLOTS: private: DataTable generateRandomData(int listCount, int valueMax, int valueCount) const; - QComboBox *createThemeBox() const; - QComboBox *createAnimationBox() const; - QComboBox *createLegendBox() const; + void populateThemeBox(); + void populateAnimationBox(); + void populateLegendBox(); void connectSignals(); QChart *createAreaChart() const; QChart *createBarChart(int valueCount) const; @@ -79,10 +80,7 @@ private: QList<QChartView *> m_charts; DataTable m_dataTable; - QComboBox *m_themeComboBox; - QCheckBox *m_antialiasCheckBox; - QComboBox *m_animatedComboBox; - QComboBox *m_legendComboBox; + Ui_ThemeWidgetForm *m_ui; }; #endif /* THEMEWIDGET_H */ diff --git a/examples/charts/chartthemes/themewidget.ui b/examples/charts/chartthemes/themewidget.ui new file mode 100644 index 00000000..9ea2bb7c --- /dev/null +++ b/examples/charts/chartthemes/themewidget.ui @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>ThemeWidgetForm</class> + <widget class="QWidget" name="ThemeWidgetForm"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>900</width> + <height>600</height> + </rect> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0" colspan="3"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="themeLabel"> + <property name="text"> + <string>Theme:</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="themeComboBox"/> + </item> + <item> + <widget class="QLabel" name="animatedLabel"> + <property name="text"> + <string>Animation:</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="animatedComboBox"/> + </item> + <item> + <widget class="QLabel" name="legendLabel"> + <property name="text"> + <string>Legend:</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="legendComboBox"/> + </item> + <item> + <widget class="QCheckBox" name="antialiasCheckBox"> + <property name="text"> + <string>Anti-aliasing</string> + </property> + <property name="checked"> + <bool>false</bool> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>themeComboBox</sender> + <signal>currentIndexChanged(int)</signal> + <receiver>ThemeWidgetForm</receiver> + <slot>updateUI()</slot> + </connection> + <connection> + <sender>antialiasCheckBox</sender> + <signal>toggled(bool)</signal> + <receiver>ThemeWidgetForm</receiver> + <slot>updateUI()</slot> + </connection> + <connection> + <sender>legendComboBox</sender> + <signal>currentIndexChanged(int)</signal> + <receiver>ThemeWidgetForm</receiver> + <slot>updateUI()</slot> + </connection> + <connection> + <sender>animatedComboBox</sender> + <signal>currentIndexChanged(int)</signal> + <receiver>ThemeWidgetForm</receiver> + <slot>updateUI()</slot> + </connection> + </connections> + <slots> + <slot>updateUI()</slot> + </slots> +</ui> |