在qt小部件应用程序的CGAl包中运行draw_polygon示例

时间:2018-11-14 10:48:29

标签: c++ qt qt-creator fedora cgal

我尝试在Qt小部件应用程序中遵循以下CGAL示例: example

main.ccp:

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

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

        return a.exec();
}

mainwindow.ccp:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_polyhedron.h>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel  Kernel;
typedef CGAL::Polyhedron_3<Kernel>                       Polyhedron;

void MainWindow::on_pushButton_clicked()
{
   QString fileName = QFileDialog::getOpenFileName(this,tr("Open .off model"), "/home", tr("*.off"));

draw_poly(fileName);
 }

void MainWindow::draw_poly(QString fileName)
{
    QByteArray inBytes;
    const char *c;
     inBytes = fileName.toUtf8();
     c = inBytes.constData();
          std::ifstream input(c);

          if (!input || !(input >> mesh) || mesh.is_empty()) {
            std::cerr << "Not a valid off file." << std::endl;
         //   return 1;
          }

          input >> mesh;

          CGAL::draw(mesh);
}

当我运行它时,它打开对话框文件以选择.off文件,然后显示以下错误:

QCoreApplication::exec: The event loop is already running

有什么帮助吗?

2 个答案:

答案 0 :(得分:2)

我在日常业务中使用Qt5,并且曾经将CGAL视为可能的应用程序基础(没有进一步朝这个方向发展-到目前为止)。因此,这个问题使我感到好奇。

我在github上浏览了CGAL的源代码,并找出了错误消息的原因

QCoreApplication::exec: The event loop is already running

发生。

为此,我从CGAL on github: Polyhedron/include/CGAL/draw_polyhedron.h复制了相关行:

template<class Polyhedron, class ColorFunctor>
void draw(const Polyhedron& apoly,
          const char* title,
          bool nofill,
          const ColorFunctor& fcolor)
{  
#if defined(CGAL_TEST_SUITE)
  bool cgal_test_suite=true;
#else
  bool cgal_test_suite=false;
#endif

  if (!cgal_test_suite)
  {
    int argc=1;
    const char* argv[2]={"polyhedron_viewer","\0"};
    QApplication app(argc,const_cast<char**>(argv));
    SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>
      mainwindow(app.activeWindow(), apoly, title, nofill, fcolor);
    mainwindow.show();
    app.exec();
  }
}

看看此源代码,很明显CGAL::draw()本身就是一个小的功能齐全的Qt应用程序,它建立了自己的QApplication实例。 OP随后尝试将CGAL::draw()嵌入她自己的Qt应用程序中。不允许多次实例化QCoreApplication的任何派生实例(根据QApplication的Qt文档):

  

对于使用Qt的任何GUI应用程序,无论应用程序在任何给定时间有0、1、2或更多窗口,都存在一个 QApplication对象。

(强调 not 我的。)

CGAL文档。在Polyhedron/draw_polyhedron.cpp中提供了一个(甚至更短的)示例来实现此目的:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_polyhedron.h>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel  Kernel;
typedef CGAL::Polyhedron_3<Kernel>                       Polyhedron;
int main(int argc, char* argv[])
{
  Polyhedron P;
  std::ifstream in1((argc>1)?argv[1]:"data/cross.off");
  in1 >> P;
  CGAL::draw(P);
  return EXIT_SUCCESS;
}

但是没有地方在正确的位置插入QFileDialog

因此,CGAL::draw()是OP(可能)打算做的错误工具–将CGAL多面体渲染嵌入Qt应用程序。为此,有必要直接使用在CGAL::draw()内部某个地方调用的东西。

所以,这对我来说似乎很合适:
在OPs Qt应用程序中将SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>设为(主或子)小部件。

然后我在github存储库中浏览了一下,以找出实际上是从哪个Qt小部件CGAL::SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>派生的,并发现了以下继承:

CGAL::SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>
                           |
                           V
                 CGAL::Basic_viewer_qt
                           |
                           V
                   CGAL::QGLViewer
                           |
            +--------------+--------------+
            |                             |
            V                             V
      QOpenGLWidget               QOpenGLFunctions

因此,CGAL::SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>可以像任何QWidget一样使用(这涉及使其成为主窗口)。它也可以成为QMainWindow的中央窗口小部件,该窗口小部件带有QAction的菜单栏/工具栏,以打开QFileDialog,请求文件路径,并以此打开文件流文件路径,然后从该文件流加载网格。

我偶然发现了另一个小细节:CGAL::Polyhedron必须在构造函数中通过常量引用赋予CGAL::SimplePolyhedronViewerQt。考虑到这一点,需要IMHO(在成功加载网格之后)通过CGAL::SimplePolyhedronViewerQt构造new实例,然后将其设置/添加到父窗口小部件。如果这不可接受,则可能有必要更深入地研究,并使用自己的实现替换CGAL::SimplePolyhedronViewerQt,并使用前者的源代码作为“备忘单”。

这是这样的应用程序的外观:

#include <fstream>

#include <QtWidgets>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_polyhedron.h>

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

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  CGAL::DefaultColorFunctorPolyhedron fColor;
  Polyhedron mesh;
  // setup UI
  QMainWindow qWin;
  QToolBar qToolbar;
  QAction qCmdLoad(QString::fromUtf8("Load File..."));
  qToolbar.addAction(&qCmdLoad);
  qWin.addToolBar(&qToolbar);
  qWin.show();
  // install signal handlers
  QObject::connect(&qCmdLoad, &QAction::triggered,
    [&qWin, &mesh, &fColor]() {
      const QString filePath = QFileDialog::getOpenFileName(
        &qWin,
        QString::fromUtf8("Open .off model"),
        QString::fromUtf8("/home"),
        QString::fromUtf8("*.off"));
      if (filePath.isEmpty()) return;
      std::ifstream fIn(filePath.toUtf8().data());
      if (!(fIn >> mesh) || mesh.is_empty()) {
        qDebug() << "Loading of" << filePath << "failed!";
        return;
      }
      qWin.setCentralWidget(
        new CGAL::SimplePolyhedronViewerQt<Polyhedron, CGAL::DefaultColorFunctorPolyhedron>(
          &qWin, mesh, "Basic Polyhedron Viewer", false, fColor));
      qWin.centralWidget()->show();
    });
  // runtime loop
  return app.exec();
}

请带着“一粒盐”来处理–我手头没有CGAL,并且无法编译/测试以上代码。

答案 1 :(得分:0)

CGAL :: draw()已经处理了Qt的东西。您正在尝试在另一个窗口中打开一个主窗口。只需在main()函数中调用CGAL :: draw(mesh)即可,一切正常。

编辑:这正是Sheff更详细地解释的。

相关问题