i want to build 5 graphs on my grid layout located on the scroll area.
Name of grid layout is "grap" But every time, when i run my code, i only see 1 graph on my widget. Down below i attached class of my graph. And i know that i will receive 5 equals graphs.
GraphBuilder.h
#ifndef GRAPHBUILDER_H
#define GRAPHBUILDER_H
#include "qcustomplot.h"
#include <QWidget>
#include <QObject>
class GraphBuilder : public QWidget
{
Q_OBJECT
public:
explicit GraphBuilder(QWidget* parent = nullptr);
~GraphBuilder();
void build();
private:
QCustomPlot* _customPlot = nullptr;
};
#endif // GRAPHBUILDER_H
GraphBuilder.cpp
#include "graphbuilder.h"
GraphBuilder::GraphBuilder(QWidget* parent)
: QWidget(parent)
{
_customPlot = new QCustomPlot(this);
}
GraphBuilder::~GraphBuilder()
{
delete _customPlot;
}
void GraphBuilder::build()
{
QVector<double> xData = {1, 2, 3, 4, 5};
QVector<double> yData = {1, 4, 9, 16, 25};
_customPlot->addGraph();
_customPlot->graph()->setData(xData, yData);
_customPlot->xAxis->setLabel("X");
_customPlot->yAxis->setLabel("Y");
_customPlot->xAxis->setRange(0, 6);
_customPlot->yAxis->setRange(0, 30);
_customPlot->replot();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "graphbuilder.h"
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void printGraph();
Ui::MainWindow *ui;
GraphBuilder* graph = nullptr;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
graph = new GraphBuilder();
printGraph();
}
MainWindow::~MainWindow()
{
delete ui;
delete graph;
}
void MainWindow::printGraph()
{
for (int i = 0; i < 5; i++)
{
ui->grap->addWidget(graph);
graph->build();
}
}
Photo->
Aucun commentaire:
Enregistrer un commentaire