#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
using namespace QtCharts;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private:
QChartView *chartview;
QChart *chart;
void CreateChartFunction();
};
#endif // MAINWINDOW_H
-----------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("chart");
CreateChartFunction();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::CreateChartFunction()
{
chartview = new QChartView(this);
chart = new QChart();
chart->setTitle("sin and cos chart test");
chartview->setChart(chart);
this->setCentralWidget(chartview);
QLineSeries *series1=new QLineSeries();
QLineSeries *series2=new QLineSeries();
series1->setName("sin curve");
series2->setName("cos curve");
chart->addSeries(series1);
chart->addSeries(series2);
qreal t=0,y1,y2,intv=0.1;
int icount=100;
for(int i=0;i
y1=qSin(t);
series1->append(t,y1);
y2=qCos(t);
series2->append(t,y2);
t=t+intv;
}
QValueAxis *axisX=new QValueAxis;
axisX->setRange(0,10);
axisX->setTitleText("time(seconds)");
QValueAxis *axisY=new QValueAxis;
axisY->setRange(-2,2);
axisY->setTitleText("values");
chart->setAxisX(axisX,series1);
chart->setAxisY(axisY,series1);
chart->setAxisX(axisX,series2);
chart->setAxisY(axisY,series2);
}