qt6 and V3d problem

Hi guys, I have built a simple visualization program in qt with OcctWindow class and the description in the OCC document. The problem is the view can be showed, but only for a second(maybe less than that). I have tried to debug it, found that the view is good until the statement "return a.exec()” was executed, after that statement was executed, the view gone missing. Hi guys, I have built a simple visualization program in qt with OcctWindow class and the description in the OCC document. The problem is the view can be showed, but only for a second(maybe less than that). I have tried to debug it, found that the view is good until the statement "return a.exec()” was executed, after that statement was executed, the view gone missing.

here is the code.

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <V3d_View.hxx>
#include <V3d_Viewer.hxx>
#include <AIS_InteractiveContext.hxx>
#include "OcctWindow.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Handle(OcctWindow) qtAspectWND;
    Handle(V3d_View) myView;
    Handle(V3d_Viewer) aViewer;
    Handle(AIS_InteractiveContext) aAISContext;
};
#endif // MAINWINDOW_H

MainWindow.cpp


#include "mainwindow.h" #include <Quantity_Color.hxx> #include <Quantity_NameOfColor.hxx> #include <Graphic3d_AspectLine3d.hxx> #include <Graphic3d_AspectMarker3d.hxx> #include <Graphic3d_AspectFillArea3d.hxx> #include <Graphic3d_MaterialAspect.hxx> #include <Aspect_InteriorStyle.hxx> #include <Graphic3d_NameOfMaterial.hxx> #include <Graphic3d_AspectText3d.hxx> #include <OpenGl_GraphicDriver.hxx> #include <Aspect_DisplayConnection.hxx> #include <V3d_View.hxx> #include <V3d_Viewer.hxx> #include <AIS_InteractiveContext.hxx> #include <TopoDS_Shape.hxx> #include <BRepPrimAPI_MakeBox.hxx> #include <AIS_Shape.hxx> #include <QDebug> Handle(OpenGl_GraphicDriver) GetOCCGraphicDriver() { static Handle(OpenGl_GraphicDriver) G_GraphicDriver = NULL; if(G_GraphicDriver.IsNull()) { Handle(Aspect_DisplayConnection) theDisp = new Aspect_DisplayConnection(); G_GraphicDriver = new OpenGl_GraphicDriver(theDisp); } return G_GraphicDriver; } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { qDebug() << "主窗口构造"; qtAspectWND = new OcctWindow(this); qtAspectWND->Map(); // 创建颜色 Quantity_Color aBlack(Quantity_NOC_BLACK); Quantity_Color aBlue(Quantity_NOC_MATRABLUE); Quantity_Color aBrown(Quantity_NOC_BROWN4); Quantity_Color aFirebrick(Quantity_NOC_FIREBRICK); Quantity_Color aForest(Quantity_NOC_FORESTGREEN); Quantity_Color aGray(Quantity_NOC_GRAY70); Quantity_Color aMyColor(0.99, 0.65, 0.31, Quantity_TOC_RGB); Quantity_Color aBeet(Quantity_NOC_BEET); Quantity_Color aWhite(Quantity_NOC_WHITE); // 创建线属性 Handle(Graphic3d_AspectLine3d) anAspectBrown = new Graphic3d_AspectLine3d(); Handle(Graphic3d_AspectLine3d) anAspectBlue = new Graphic3d_AspectLine3d(); Handle(Graphic3d_AspectLine3d) anAspectWhite = new Graphic3d_AspectLine3d(); anAspectBrown->SetColor(aBrown); anAspectBlue->SetColor(aBlue); anAspectWhite->SetColor(aWhite); // 创建标记属性 Handle(Graphic3d_AspectMarker3d) aFirebrickMarker = new Graphic3d_AspectMarker3d(); aFirebrickMarker->SetColor(aFirebrick); aFirebrickMarker->SetScale(1.0f); aFirebrickMarker->SetType(Aspect_TOM_BALL); // 创建面属性 Handle(Graphic3d_AspectFillArea3d) aFaceAspect = new Graphic3d_AspectFillArea3d(); Graphic3d_MaterialAspect aBrassMaterial(Graphic3d_NameOfMaterial_Brass); Graphic3d_MaterialAspect aGoldMaterial(Graphic3d_NameOfMaterial_Gold); aFaceAspect->SetInteriorStyle(Aspect_IS_SOLID); aFaceAspect->SetInteriorColor(aMyColor); aFaceAspect->SetDistinguishOn(); aFaceAspect->SetFrontMaterial(aGoldMaterial); aFaceAspect->SetBackMaterial(aBrassMaterial); // 创建文字属性 Handle(Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d(aForest, Font_NOF_MONOSPACE, 1.0, 0.0); // 创建视口 Handle(Aspect_DisplayConnection) aDisplayConnect = new Aspect_DisplayConnection(); Handle(OpenGl_GraphicDriver) aGraphicDriver = ::GetOCCGraphicDriver(); if(aGraphicDriver.IsNull()) { qDebug() << "OPENGL ERROR"; } aViewer = new V3d_Viewer(aGraphicDriver); // 创建交互式上下文 aAISContext = new AIS_InteractiveContext(aViewer); // 设置默认灯光 aViewer->SetDefaultLights(); aViewer->SetLightOn(); aViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK); // 创建视图 myView = aViewer->CreateView(); myView->SetWindow(qtAspectWND); // 创建形状 TopoDS_Shape aShape = BRepPrimAPI_MakeBox(10, 20, 30).Solid(); Handle(AIS_Shape) aBoxAISShape = new AIS_Shape(aShape); aAISContext->Display(aBoxAISShape,true); myView->FitAll(); myView->Update(); } MainWindow::~MainWindow() { qDebug() << "主窗口销毁"; }

The main function is a typical and simple qt main function with nothing new.

lei hu's picture

Oh, I don't know what happend to the format, sorry for that annoying head file code.
Thank you for all of your help very much.

言 诺's picture

may be the first argument of OcctWindow constructor is not parent widget

言 诺's picture

you can use the ApplicationCommonWindow class as mult documents window

lei hu's picture

Thank you for you reponse. I will try it later. For now, I am getting involved with a class occView which is created by eryar on GitHub. https://github.com/eryar/occQt