CGAL visualizer in a Qt-GUI with and existing QWidget

Viewed 44

I was trying to realize the representation of a mesh in a Qt based application. Looking at information from various forums I managed to come up with this working solution using a basic main.cpp:

#include <QApplication>

#include <QWidget>
#include <QMainWindow>

#include <fstream>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Qt/Basic_viewer_qt.h>
#include <CGAL/draw_face_graph.h>
#include <CGAL/Qt/init_ogl_context.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel  Kernel;
typedef CGAL::Polyhedron_3<Kernel>                       Polyhedron;


int main(int argc, char *argv[]) {
  
    QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);   // Elimina un error que aparece por ahí raro
    QApplication a(argc, argv);

    CGAL::Qt::init_ogl_context(4,4);
    Polyhedron mesh;

    // Configuración del UI
    QMainWindow qWin;
    qWin.setWindowTitle("Ventana de pruebas");
    qWin.show();
    qWin.resize(1000,1000);

    // Cargar el nombre del archivo
    QString fname = argv[1];
    if (fname.isEmpty()) {
        std::cout << "No se ha podido cargar el archivo." << std::endl;
        return 0;
    }
    std::ifstream fIn(fname.toUtf8().data());
    if (!(fIn >> mesh) || mesh.is_empty()) {
        std::cout << "Loading of" << fname.toStdString() << "failed!" << std::endl;
        return 0;
    }
    CGAL::DefaultColorFunctorFaceGraph fColor;
    qWin.setCentralWidget(new CGAL::SimpleFaceGraphViewerQt(&qWin,mesh,"Basic Polyhedron Viewer",false, fColor));
    qWin.centralWidget()->show();

    return QApplication::exec();
}

But what I want to do is to realize the representation in an existing QWidget of a QMainWindow, let's call it for example qViewer (accessible as ui->qViewer). In this case I rewrite de code as:

main.cpp

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

#include <QWidget>
#include <QMainWindow>

int main(int argc, char *argv[]) 
{
    QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
    QApplication a(argc, argv);

    MainWindow w;
    w.show();
    return a.exec();
}

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <fstream>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Qt/Basic_viewer_qt.h>
#include <CGAL/draw_face_graph.h>
#include <CGAL/Qt/init_ogl_context.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel  Kernel;
typedef CGAL::Polyhedron_3<Kernel>                       Polyhedron;

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
    connect(ui->actionDraw,SIGNAL(triggered()),this,SLOT(onActionDraw()));
}

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

void MainWindow::onActionDraw() {
    std::cout << "Button Clicked" << std::endl;

    CGAL::Qt::init_ogl_context(4,4);
    Polyhedron mesh;

    // Cargar el nombre del archivo
    QString fname = qApp->arguments().at(1);
    if (fname.isEmpty()) {
        std::cout << "No se ha podido cargar el archivo." << std::endl;
        return;
    }
    std::ifstream fIn(fname.toUtf8().data());
    if (!(fIn >> mesh) || mesh.is_empty()) {
        std::cout << "Loading of" << fname.toStdString() << "failed!" << std::endl;
        return;
    }
    CGAL::DefaultColorFunctorFaceGraph fColor;
    ui->qViewer = new CGAL::SimpleFaceGraphViewerQt(this,mesh,"Basic Polyhedron Viewer",false, fColor));
}

But, if I execute it I get the following error:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Does someone know how can I solve this and represent the mesh inside this QWidget? Thanks

0 Answers
Related