使用QChart须要在*.pro中添加app
QT+=charts 在*.h文件中添加使用Qt中预先定义好的宏:
[cpp] view plain copyide
QT_CHARTS_BEGIN_NAMESPACE
classQChartView;
classQChart;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE 或者使用
usingnamespaceQtCharts
柱状图:
mainwindow.h
#include
#include
#include
#include
//#include
//#include
//#include
//#include
#include
#include
//#include
//#include
#include
#include
#include
#include
#include
QT_CHARTS_BEGIN_NAMESPACE
classQChartView;
classQChart;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
namespaceUi{
classMainWindow;
}
classMainWindow:publicQMainWindow
{
Q_OBJECT
public:
explicitMainWindow(QWidget*parent=0);
~MainWindow();
private:
voidinitChart();
privateslots:
voidsltpushButton();
voidsltpushButton_2();
voidsltpushButton_3();
voidsltCategoriesChanged();
private:
Ui::MainWindow*ui;
QChart*m_typeChart;
QChartView*m_typeView;
QBarCategoryAxis*m_typeAxis;
QValueAxis*m_typeAxisY;
QBarSeries*m_barSeries;
QBarSet*m_barSet;
};
mainwindow.cpp动画
#include"mainwindow.h"
#include"ui_mainwindow.h"
MainWindow::MainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi::MainWindow)
{
ui->setupUi(this);
initChart();
}
MainWindow::~MainWindow()
{
deleteui;
}
voidMainWindow::initChart()
{
m_typeChart=newQChart();
m_typeChart->setAnimationOptions(QChart::SeriesAnimations);//动画效果
m_barSet=newQBarSet("eg")