1、安装并配置好项目后,填写如下代码:
cpp
#pragma once
#include <Standard_Handle.hxx>
#include <V3d_Viewer.hxx>
#include <OpenGl_GraphicDriver.hxx>
#include <WNT_Window.hxx>
#include <V3d_View.hxx>
#include <AIS_InteractiveContext.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <TopoDs_Shape.hxx>
#include <AIS_Shape.hxx>
#include <QtWidgets/QMainWindow>
#include "ui_OCC1.h"
#include <QDebug>
class V3d_Viewer;
class V3d_View;
class AIS_InteractiveContext;
class AIS_ViewController;
class OCC1 : public QMainWindow
{
Q_OBJECT
public:
OCC1(QWidget *parent = nullptr);
~OCC1();
void paintEvent(QPaintEvent* event);
QPaintEngine* paintEngine() const;
private:
Ui::OCC1Class ui;
Handle(V3d_Viewer) viewer;
Handle(V3d_View) view;
Handle(WNT_Window) window;
Handle(AIS_InteractiveContext) context;
};
cpp
#include "OCC1.h"
OCC1::OCC1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
Handle(Aspect_DisplayConnection) hAspect_DisplayConnect = new Aspect_DisplayConnection;
Handle(OpenGl_GraphicDriver) driver = new OpenGl_GraphicDriver(hAspect_DisplayConnect);
viewer = new V3d_Viewer(driver);
view = viewer->CreateView();
WId window_handle = (WId)winId();
//WId win_handle = winId();
window = new WNT_Window((Aspect_Handle)window_handle);
view->SetWindow(window);
if (!window->IsMapped())
{
window->Map();//打开窗口
}
view->SetBackgroundColor(Quantity_NOC_BLACK);
view->MustBeResized();
view->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_ZBUFFER);
viewer->SetDefaultLights();
setAttribute(Qt::WA_PaintOnScreen);
context = new AIS_InteractiveContext(viewer);
context->SetDisplayMode(AIS_Shaded, Standard_True);
}
OCC1::~OCC1()
{}
void OCC1::paintEvent(QPaintEvent* event)
{
view->Redraw();
}
QPaintEngine* OCC1::paintEngine() const
{
return 0;
}
cpp
#include "OCC1.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OCC1 w;
w.show();
return a.exec();
}
2、运行结果: