1.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_NAMESPACE QT_CHARTS_BEGIN_NAMESPACE class QChartView; class QChart; QT_CHARTS_END_NAMESPACE typedef QPair<QPointF, QString> Data; typedef QList<Data> DataList; typedef QList<DataList> DataTable; QT_CHARTS_USE_NAMESPACE class 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 */
1)关于QT_CHARTS_USE_NAMESPACE
相当于using namespace QtCharts
2)关于Q_OBJECT
信号和槽是Qt应用开发的基础,它可是将两个毫无关系的对象连接在一起,槽和普通的C++函数是一样的,只是当它和信号连接在一起后,当发送信号的时候,槽会自动被调用。只有加入了Q_OBJECT,你才能使用QT中的signal和slot机制。
3)关于关键字explicit
首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况下即声明为implicit(隐式)。
显式是强制转换,而隐式是编译器自动转换的,一般同一数据类型(如byte,int,float,double),由低级转为高级为隐式(如int转为float或double),而高级转为低级为强制类型转换,即显式转换,须用户在代码中写的(如float转为int)。
2.ui_Themewidget.h
#ifndef UI_THEMEWIDGET_H
#define UI_THEMEWIDGET_H #include <QtCore/QVariant> #include <QtWidgets/QApplication> #include <QtWidgets/QCheckBox> #include <QtWidgets/QComboBox> #include <QtWidgets/QGridLayout> #include <QtWidgets/QHBoxLayout> #include <QtWidgets/QLabel> #include <QtWidgets/QSpacerItem> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_ThemeWidgetForm { public:
//布局管理器 QGridLayout *gridLayout;
//水平布局 QHBoxLayout *horizontalLayout;
//标签 QLabel *themeLabel;
//下拉框 QComboBox *themeComboBox; QLabel *animatedLabel; QComboBox *animatedComboBox; QLabel *legendLabel; QComboBox *legendComboBox;
//复选钮 QCheckBox *antialiasCheckBox;
//空白区 QSpacerItem *horizontalSpacer; void setupUi(QWidget *ThemeWidgetForm) { if (ThemeWidgetForm->objectName().isEmpty()) ThemeWidgetForm->setObjectName(QString::fromUtf8("ThemeWidgetForm")); ThemeWidgetForm->resize(900, 600); gridLayout = new QGridLayout(ThemeWidgetForm); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); horizontalLayout = new QHBoxLayout(); horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); themeLabel = new QLabel(ThemeWidgetForm); themeLabel->setObjectName(QString::fromUtf8("themeLabel")); horizontalLayout->addWidget(themeLabel); themeComboBox = new QComboBox(ThemeWidgetForm); themeComboBox->setObjectName(QString::fromUtf8("themeComboBox")); horizontalLayout->addWidget(themeComboBox); animatedLabel = new QLabel(ThemeWidgetForm); animatedLabel->setObjectName(QString::fromUtf8("animatedLabel")); horizontalLayout->addWidget(animatedLabel); animatedComboBox = new QComboBox(ThemeWidgetForm); animatedComboBox->setObjectName(QString::fromUtf8("animatedComboBox")); horizontalLayout->addWidget(animatedComboBox); legendLabel = new QLabel(ThemeWidgetForm); legendLabel->setObjectName(QString::fromUtf8("legendLabel")); horizontalLayout->addWidget(legendLabel); legendComboBox = new QComboBox(ThemeWidgetForm); legendComboBox->setObjectName(QString::fromUtf8("legendComboBox")); horizontalLayout->addWidget(legendComboBox); antialiasCheckBox = new QCheckBox(ThemeWidgetForm); antialiasCheckBox->setObjectName(QString::fromUtf8("antialiasCheckBox")); antialiasCheckBox->setChecked(false); horizontalLayout->addWidget(antialiasCheckBox); horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); horizontalLayout->addItem(horizontalSpacer); gridLayout->addLayout(horizontalLayout, 0, 0, 1, 3); retranslateUi(ThemeWidgetForm); QObject::connect(themeComboBox, SIGNAL(currentIndexChanged(int)), ThemeWidgetForm, SLOT(updateUI())); QObject::connect(antialiasCheckBox, SIGNAL(toggled(bool)), ThemeWidgetForm, SLOT(updateUI())); QObject::connect(legendComboBox, SIGNAL(currentIndexChanged(int)), ThemeWidgetForm, SLOT(updateUI())); QObject::connect(animatedComboBox, SIGNAL(currentIndexChanged(int)), ThemeWidgetForm, SLOT(updateUI())); QMetaObject::connectSlotsByName(ThemeWidgetForm); } // setupUi void retranslateUi(QWidget *ThemeWidgetForm) { themeLabel->setText(QCoreApplication::translate("ThemeWidgetForm", "Theme:", nullptr)); animatedLabel->setText(QCoreApplication::translate("ThemeWidgetForm", "Animation:", nullptr)); legendLabel->setText(QCoreApplication::translate("ThemeWidgetForm", "Legend:", nullptr)); antialiasCheckBox->setText(QCoreApplication::translate("ThemeWidgetForm", "Anti-aliasing", nullptr)); (void)ThemeWidgetForm; } // retranslateUi }; namespace Ui { class ThemeWidgetForm: public Ui_ThemeWidgetForm {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_THEMEWIDGET_H