September 20, 2022
QT

How to add statistics in QT

How to add statistics in QT?

 

What do you need?

Create hero.h and Hero.cpp

1.Hero.h

#include<QString>
#include<QSqlQueryModel>
#include <QChartView>
#include<QtCharts>
#include<QChart>

class Hero{

QString Type;
public:

QString getType()
{return Type;}
void setType(QString t)
{Type=t;}

Hero();
Hero(QString);

QChart *statistic();
};

 

2. Hero.cpp

 

#include “Hero.h”
#include”connection.h”
#include<QDebug>
#include<QSqlDatabase>
#include<QSqlQuery>
#include<QSqlQueryModel>
Hero::Hero()

{

}

Hero::Hero(QString Type)
{
this->Type=Type;

}

QChart * Hero::statistic()

{

int batman=0, ironman=0;

QSqlQuery query;

query.prepare(“select type from Hero”);

query.exec();

while(query.next())

{

if(query.value(0).toString() == “batman”)

{

batman++;

}

else if(query.value(0).toString() == “ironman”)

{

ironman++;

}

}

QPieSeries *series = new QPieSeries();

series->append(“batman”,batman);

series->append(“ironman”,ironman);

QChart * chart=new QChart();

chart->addSeries(series);

chart->setTitle(“stats”);

return chart;

}

3.mainwindows.cpp

Add a bouton and go to slot and copy this:

Hero H;
QChartView * chartView=new QChartView(H.statistic());
chartView ->setParent(ui->statisticFrame);
chartView->setFixedSize(ui->statisticFrame->size());

ui->stackedWidget_resources_humaines->setCurrentIndex(your page number);

 

Configuration

1-Change class hero and type “batman” “ironman” and setCurrentIndex(your page number) by your own data.

2-Don’t forget to add those includes to mainwindow.h

#include <QtCharts>
#include <QLineSeries>
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>

#include “hero”

2-That’s all.

Leave a Reply

Your email address will not be published.