首页 > 解决方案 > 在 qt 小部件应用程序的 CGAl 包中运行 draw_polygon 示例

问题描述

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

主要.ccp:

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

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

        return a.exec();
}

主窗口.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

有什么帮助吗?

标签: c++qtqt-creatorfedoracgal

解决方案


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

我在github上翻了一下CGAL的源码,发现了错误信息的原因

QCoreApplication::exec: The event loop is already running

发生。

为此,我从github 上的 CGAL 复制了相关行: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多次实例化任何衍生物(根据 Qt doc. of QApplication):

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

(强调不是我的。)

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 repo 以找出 Qt 小部件CGAL::SimplePolyhedronViewerQt<Polyhedron, ColorFunctor>实际上源自哪个 Qt 小部件,并找到了以下继承:

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

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

我偶然发现了另一个小细节:CGAL::Polyhedron必须CGAL::SimplePolyhedronViewerQt在构造函数中和 const 引用中提供。考虑到这一点,恕我直言,有必要(在成功加载网格之后)构造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,无法编译/测试上面的代码。


推荐阅读