0

I have a problem with MathGL library and creating MathGL instance in the application. Every time I try to run it there is an error saying that QApplication must be constructed before QWidget (from which the class QMathGL inherits). Below you can find my code of main and all the functions connected with the MainWindow class:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include "surface.h"
#include "error.h"
#include <QString>
#include <QFileDialog>
#include <GL/glut.h>
#include <cmath>
#include <fstream>
#include <mgl2/qmathgl.h>

surface graph;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_draw_clicked()
{
    string s;
    bool correct = 1;
    error *errorWindow = new error(this);
    s = ui->eqEdit->text().toStdString();
    period fPeriod = period(ui->upEdit->text().toDouble(),ui->lowEdit->text().toDouble());
    functionSum fSum = functionSum(s,fPeriod,ui->accEdit->text().toDouble());
    for (int i = 0; i < fSum.accuracy; i++)
    {
        for (int j = 0; j < fSum.accuracy; j++)
        {
            if(isnan(fSum.zValues[i][j]) || isinf(fSum.zValues[i][j]))
            {
                correct = 0;
            }
        }
    }
    if(!correct)
    {
        errorWindow->show();
    }
    graph = surface(fSum);
}


void MainWindow::on_save_clicked()
{
    QString plik;
    string splik;
    plik = QFileDialog::getSaveFileName(this,"Zapisz plik",QString(),"Pliki tekstowe (*.txt)");
    splik = plik.toStdString();
    ofstream saver;
    saver.open(splik.c_str());
    if (saver.is_open())
    {
        for(int i = 0; i < graph.points.size(); i++)
        {
            saver << graph.points[i].x << " " << graph.points[i].y << " " << graph.points[i].z << endl;
        }
        saver.close();
    }
}

void MainWindow::on_load_clicked()
{
    QString plik;
    string splik;
    plik = QFileDialog::getOpenFileName(this,"Otwórz plik",QString(),"Pliki tekstowe (*.txt)");
    splik = plik.toStdString();
    ifstream loader;
    loader.open(splik.c_str());
    graph.points.clear();
    if(loader.is_open())
    {
        while(!loader.eof())
        {
            point a(0,0,0);
            loader >> a.x >> a.y >> a.z;
            graph.points.push_back(a);
        }
        loader.close();
    }
}

void MainWindow::on_plot_clicked()
{
     int a = sqrt(graph.points.size());
     mglData x(a,a);
     mglData y(a,a);
     mglData z(a,a);
     long long int k = 0;
     for (long long int i = 0;i<sqrt(graph.points.size());i++)
     {
         for (long long int j = 0;j<sqrt(graph.points.size());j++)
         {
             x.a[i+a*j] = graph.points[k].x;
             y.a[i+a*j] = graph.points[k].y;
             z.a[i+a*j] = graph.points[k].z;
             k++;
         }
     }
     mglGraph plot;
     plot.Rotate(50,60);
     plot.Light(true);
     plot.Surf(x,y,z);
     QMathGL plotter;
     plotter.setGraph(&plot);
     plotter.show();
}

main.cpp

#include "mainwindow.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
Oxide
  • 11
  • 1
  • 4
  • Do the answers to [this question](http://stackoverflow.com/q/23804238/1329652) help at all? It looks like the `surface` class is a global variable and either inherits from `QWidget`, or tries to instantiate a `QWidget`. If that's not the problem, inspect all of your other source files and ensure that no widgets are global variables. – Kuba hasn't forgotten Monica Jul 02 '15 at 17:51
  • Surface doesn't inherit from QWidget. It's just a class representing a set of points. – Oxide Jul 02 '15 at 18:16
  • OK. Now start minimizing the code. Your goal is to have a single file (just `main.cpp`) test case that is fully minimized. In other words, the problem should disappear if you remove any one statement from the code. Usually, while you're preparing such minimization, you'll find the source of the problem yourself. The code that you're showing doesn't appear to be the source of this problem, though. – Kuba hasn't forgotten Monica Jul 02 '15 at 18:18
  • Side notes: 1. You won't see the plot since you create a `Plotter` locally, and then it promptly exits the scope as `on_plot_clicked` returns. 2. You don't have to `close()` an ifstream - that's the whole point of C++ RAII pattern coupled with automatic storage of instances. – Kuba hasn't forgotten Monica Jul 02 '15 at 18:20

0 Answers0