qt 动态生成柱状图

cpp文件

#include "mform.h"
#include "ui_mform.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

QVectordata0;
QVectordata1;
QVectordata2;
QVectordata3;

using namespace QtCharts;

MForm::MForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MForm)
{
    ui->setupUi(this);
    for (int i = 0; i < 6; ++i) {
        data0.push_front(0);
        data1.push_front(0);
        data2.push_front(0);
        data3.push_front(0);
    }
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        data0.push_front(ui->lineEdit_1->text().toInt());
        data1.push_front(ui->lineEdit_2->text().toInt());
        data2.push_front(ui->lineEdit_3->text().toInt());
        data3.push_front(ui->lineEdit_4->text().toInt());

        QBarSet *set0 = new QBarSet("日光(min)");
        QBarSet *set1 = new QBarSet("UV(min)");
        QBarSet *set2 = new QBarSet("日光(month)");
        QBarSet *set3 = new QBarSet("UV(month)");

        QVector::iterator it1 = data0.begin();
        QVector::iterator it2 = data1.begin();
        QVector::iterator it3 = data2.begin();
        qDebug()<::iterator it4 = data3.begin();it4append(set0);
        series->append(set1);
        series->append(set2);
        series->append(set3);
        QChart *c = new QChart();
        c->setTitle("光照UV强度");
        c->addSeries(series);
        ui->graphicsView->setChart(c);
        c->legend()->setVisible(true);
        c->setAnimationOptions(QChart::SeriesAnimations);
        ui->graphicsView->setRenderHint(QPainter::Antialiasing);
        c->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值)
        QValueAxis *axisX = new QValueAxis();//轴变量、数据系列变量,都不能声明为局部临时变量
        QValueAxis *axisY = new QValueAxis();//创建X/Y轴
        axisX->setRange(0, 6);
        axisY->setRange(0, 10);//设置X/Y显示的区间
        c->setAxisX(axisX);
        c->setAxisY(axisY);//设置chart的坐标轴
        series->attachAxis(axisX);
        c->legend()->setVisible(true); //设置图例为显示状态
        c->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部

    });
}

MForm::~MForm()
{
    delete ui;
}

.h文件

#ifndef MFORM_H
#define MFORM_H

#include 
#include 
namespace Ui {
class MForm;
}

class MForm : public QWidget
{
    Q_OBJECT

public:
    explicit MForm(QWidget *parent = 0);
    ~MForm();

private:
    Ui::MForm *ui;
};

#endif // MFORM_H

ui文件

qt 动态生成柱状图_第1张图片

qt 动态生成柱状图_第2张图片

你可能感兴趣的:(qt,qt,命令模式,开发语言)