QCustomPlot Discussion and Comments

Plot a graph in a new windowReturn to overview

Hi everybody,
I want to plot a graph when I click on the "Generate button" but I get an error: "undefined reference to QCustomPlot::addGraph ...". The program runs when I remove the graph.
Thank you for your help.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets>
#include <QtGui>
#include <QTimer>
#include "qcustomplot.h"


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    void setupQuadraticDemo(QCustomPlot *customPlot);

private slots:
    void generateGraph();
    void open();

private:
    QPushButton *ouvrir;
    QPushButton *quitter;
    QPushButton *generate;
    QLineEdit *horizon;
    QLineEdit *date;
    QComboBox *liste;
    QWidget fenetre;
    QCustomPlot *graphe;

};

#endif // MAINWINDOW_H


mainwindow.cpp

#include <QtWidgets>
#include <QApplication>
#include "mainwindow.h"
// #include "graphwindow.h"

#include <QDebug>
#include <QDesktopWidget>
#include <QScreen>
#include <QMessageBox>
#include <QMetaEnum>
#include "qcustomplot.h"

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        setGeometry(400, 250, 542, 390);

// Création des layouts et des widgets
    // Groupe : Ouverture du fichier
     ouvrir = new QPushButton("&Ouvrir");
     QHBoxLayout *openLayout = new QHBoxLayout;
     openLayout->setAlignment(Qt::AlignLeft);
     openLayout->addWidget(ouvrir);
     QGroupBox *groupOpen = new QGroupBox("Ouverture du fichier source contenant la série temporelle");
     groupOpen->setLayout(openLayout);
    // openLayout->addWidget(groupOpen);

    // Groupe : Paramètres
     date = new QLineEdit;
     horizon = new QLineEdit;
     QFormLayout *definitionLayout = new QFormLayout;
     definitionLayout->addRow("&Date de début de prévision :", date);
     definitionLayout->addRow("&Horizon :", horizon);
     QGroupBox *groupDefinition = new QGroupBox("Paramètres de la prévision");
     groupDefinition->setLayout(definitionLayout);

    // Groupe : Méthode d'estimation
     liste = new QComboBox;
     liste->addItem("Norme 1");
     liste->addItem("Norme 2");
     QFormLayout *methodeLayout = new QFormLayout;
     methodeLayout->addRow("Méthode des plus proches &voisins :", liste);
     QGroupBox *groupMethod = new QGroupBox("Méthodes d'estimation");
     groupMethod->setLayout(methodeLayout);

     // Layout : boutons du bas (générer, quitter...)
     generate = new QPushButton("Tracer la &prévision");
     quitter = new QPushButton("&Quitter");
     QHBoxLayout *boutonsLayout = new QHBoxLayout;
     boutonsLayout->setAlignment(Qt::AlignRight);
     boutonsLayout->addWidget(generate);
     boutonsLayout->addWidget(quitter);

     // Définition du layout principal, du titre de la fenêtre, etc.
     QVBoxLayout *layoutPrincipal = new QVBoxLayout;
     layoutPrincipal->addWidget(groupOpen);
     layoutPrincipal->addWidget(groupDefinition);
     layoutPrincipal->addWidget(groupMethod);
     layoutPrincipal->addLayout(boutonsLayout);
     setLayout(layoutPrincipal);
     setWindowTitle("Prévision Séries Temporelles");
     setWindowIcon(QIcon("icone.png"));
     resize(400, 450);

// Connexions des signaux et des slots
     connect(ouvrir, SIGNAL(clicked()), this, SLOT(open()));
     connect(quitter, SIGNAL(clicked()), qApp, SLOT(quit()));
     connect(generate, SIGNAL(clicked()), this, SLOT(generateGraph()));
}

void MainWindow::open()
{
QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier",QString(), "Text Files (*.txt)");
QMessageBox::information(this, "Fichier", "Vous avez sélectionné :\n" + fichier);
}

void MainWindow::generateGraph()
{
    if (ouvrir->text().isEmpty())
    {
    QMessageBox::critical(this, "Erreur", "Veuillez entrer au moins un nom de classe");
    return; // Arrêt de la méthode
    }

    //graphe = new QCustomPlot(this);
    setupQuadraticDemo(graphe);

}

void MainWindow::setupQuadraticDemo(QCustomPlot *customPlot)
{
  // generate some data:
  QVector<double> x(101), y(101); // initialize with entries 0..100
  for (int i=0; i<101; ++i)
  {
    x[i] = i/50.0 - 1; // x goes from -1 to 1
    y[i] = x[i]*x[i];  // let's plot a quadratic function
  }
  // create graph and assign data to it:
  customPlot->addGraph();
  customPlot->graph(0)->setData(x, y);
  // give the axes some labels:
  customPlot->xAxis->setLabel("x");
  customPlot->yAxis->setLabel("y");
  // set axes ranges, so we see all data:
  customPlot->xAxis->setRange(-1, 1);
  customPlot->yAxis->setRange(0, 1);
  customPlot->replot();
}

Did you add qcustomplot.cpp/.h to your project file as described in the "setting up" tutorial?

Other than that: In MainWindow::generateGraph, you call setupQuadraticDemo with graphe, but have never created that instance. The new QCustomPlot(this) statement that's currently commented out probably won't do what you want, because this would just create a QCustomPlot instance somewhere on your main window at each click of the generate graph button. You need to at least position the graph widget somewhere in your main window or a QLayout. If you want to open it in a new window, pass 0 as parent (QWidgets then appear as individual dialogs).