Visualization with Qt - What is wrong in my code ?

Hello,

I'm new to OpenCascade, I need help in viewing an object in a qt widget, below you can see the code for that, it is very basic, the bottle was created as in the tutorial (I think it is the only Real tutorial for openCascade).

So, what's wrong in my code?

#include <QtCore>
#include <QtGui>
#include <makeBottle.h>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // create a Qt window
    QWidget *window = new QWidget();
    
    // create a bottle (TopoDS_Shape)
    TopoDS_Shape myBottle = MakeBottle(70., 120., 30.);
    
    // th DisplayDonnection
    Handle(Aspect_DisplayConnection) aDisplayConnection;
    
    // the GraphicDriver
    Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);
    
    // creating the Viewer
    Handle(V3d_Viewer) aViewer = new V3d_Viewer(aGraphicDriver, Standard_ExtString("my3dViewer"));
    
    // creating a View
    Handle(V3d_View) aView = aViewer->CreateView();

    // creating an Xw_Window (I'm on Ubuntu)
    Handle(Xw_Window) aWindow;
    aWindow = new Xw_Window(aDisplayConnection, window->winId());

    // the Interactive context
    Handle(AIS_InteractiveContext) aContext = new AIS_InteractiveContext(aViewer);
    Handle(AIS_Shape) anAISShape = new AIS_Shape(myBottle);
    aContext->Display(anAISShape);

    aView->SetWindow(aWindow);

    window->show();
    return a.exec();
}

When running the code I get this error:

terminate called after throwing an instance of 'Aspect_WindowDefinitionError'

The program has unexpectedly finished.

I'm using Occt 6.8.0 and Qt 4.8 on Linux Ubuntu 14.04 32bit.

As I see there are many tutorials about geometric manipulation of shapes, but the there is no one tutorial that explains the visualization in a GUI (I've read nearly all the available tutorials and posts in this forum, but nothing works correctly !).

jiping xin's picture

I have the same problem. Did you solve this problem?

jiping xin's picture

the problem is from " aWindow = new Xw_Window(aDisplayConnection, window->winId());"

but i don't know why

it seems that few people use ubuntu + occ + qt

Mat Pen's picture

I was just struggling with this issue, and finally found the solution in one of the examples (see file DocumentCommon.cxx line 34).

As jiping xin pointed out, the program crashes on the line "aWindow = new Xw_Window(...)", the problem being that aDisplayConnection points to invalid data.

This is apparently expected on Windows and Mac, but for Linux it has to be set to the current display. The solution is therefore to add the following line just after the definition of "aDisplayConnection":

Handle(Aspect_DisplayConnection) aDisplayConnection;

aDisplayConnection = new Aspect_DisplayConnection(qgetenv("DISPLAY").constData());

Hope this helps!

My specs: Ubuntu 15.10, Qt 5.5, OpenCascade 6.9.1 built with AutoMake from tarball.

PS: for completeness, and in the hope that it saves hours of searching to someone else, I am attaching a minimal example that I adapted from the one kindly provided by Daniel Neander in this post. Please notice the above modification, the new definition of "Handle(Xw_Window) hWnd" and the "#undef" preprocessor instructions in the "occview.h" file. Do remember to adapt the location of your installation of opencascade in the "occt.pri" file, to set the CASROOT environment variable (can be done in QtCreator) and (for your future projects) to keep the "QT += opengl" addition in "BasicOCCViewer.pro".

jiping xin's picture

Hi I also solved this problem. It looks that there is a crash for Qt + Opengl and OCCT + opengl.

In the example from OCCT, they didn't use QT+=opengl and qglwidget.

I also attached a very simple example by myself which simplify the example from OCC.

Attachments: 
gaurav178's picture

Hi,

Thanks for the BasicOCCViewer-Linux project. It helped me immensely to get started.