about the compiling order of qt5 and opencascade

I try to figure out how to implement a qt view  for opencascade v3d_viewer.

qt5 ask for their header file should be call first.  But if put  #include <OpenGl_GraphicDriver.hxx> below #include <QGLWidget>  , it will get error.

Finally,  I end with this occview.h  file:

#include <QGLWidget>
#include <QPaintEvent>
#include <QResizeEvent>

//Opencascade includes
#include <AIS_InteractiveContext.hxx>
#include <V3d_View.hxx>
#include <V3d_Viewer.hxx>

class OccView : public QGLWidget
{

.....

and this occview.cpp:
 

// QMouseEvent and QRubberBand should above OpenGl_GraphicDriver.hxx
#include <QMouseEvent>
#include <QRubberBand>

//OpenGl_GraphicDriver.hxx should above occview.h
#include <OpenGl_GraphicDriver.hxx>
#include "occview.h"

#include <Aspect_DisplayConnection.hxx>
#include <Xw_Window.hxx>

static Handle(Graphic3d_GraphicDriver)& GetGraphicDriver()
{
  static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
  return aGraphicDriver;
}

OccView::OccView(QWidget *parent)
    : QGLWidget(parent)
{

....

I passed the compiling.  but I dont know why.

Anyone can give me some hint?

Kirill Gavrilov's picture

This is a common issue with windows.h and GL.h inclusion:

  • windows.h handles various macros like NOMINMAX disabling large portions of definitions.
    As consequence, including headers with and without such disabling macros makes result order-dependent (and some files really need these disabled declarations).
  • GL.h relies on windows.h (on Windows) and in addition there are different ways adding declarions (functions, types and defines) of newer OpenGL versions.
    OCCT and Qt have own dedicated declarations of newer OpenGL staff, conflicting to each other and leading to various compilation errors when mixed (like QGLWidget.h and OpenGl_GraphicDriver.h).

So, the general advice is:

  • Find headers inclusion order that works, when possible.
  • Avoid inclusion of GL stuff wrapped by different libraries like OCCT and Qt within the same C++ header/source.
    Split code into pieces so that each cpp includes only a single GL wrapper (use forward declarations in header files when possible).
haidong ma's picture

Kirill Gavrilov, thanks for your fast answer.

It save me a lot of time.