0

我在使用 MathGL 库和在应用程序中创建 MathGL 实例时遇到问题。每次我尝试运行它时,都会出现一个错误,指出必须在 QWidget 之前构造 QApplication(QMathGL 类继承自它)。您可以在下面找到我的 main 代码以及与 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();
}

主文件

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


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
4

0 回答 0