Qt+Occ创建3D环境

 occview窗口类继承于Qwideget,用过Qt的QGraphicsview和Scene视图-场景架构的可以将occview窗口类看作是view和Scene的结合体。对3D环境中视图的平移、旋转、放缩等等全是通过occview类的鼠标事件实现的;而且生成的3D图元都是经过occview的交互式上下文来显示。下面是occview窗口类的代码。

一.h文件

#ifndef OCCVIEW_H
#define OCCVIEW_H

#include <QWidget>
#include <QWidget>
#include<QDebug>
#include<QVector>
#include<AIS_InteractiveContext.hxx>
#include<OpenGl_GraphicDriver.hxx>
#include<V3d_View.hxx>
#include<V3d_Viewer.hxx>
#include<WNT_Window.hxx>
#include<Quantity_NameOfColor.hxx>
#include<gp_Pnt2d.hxx>
#include<QpointF>
#include<TopoDS_Shape.hxx>
#include<ProjLib.hxx>
#include<ElSLib.hxx>
#include <AIS_Manipulator.hxx>
#include<TopoDS_Face.hxx>
#include<BRepBuilderAPI_MakeFace.hxx>
#include<AIS_Shape.hxx>
namespace Ui {
class OccView;
}

class OccView : public QWidget
{
    Q_OBJECT

public:
    explicit OccView(QWidget *parent = 0);
     QPaintEngine *paintEngine() const;
    ~OccView();
Handle(V3d_View) m_view;
private:
    Ui::OccView *ui;
private:
    Handle(AIS_InteractiveContext) m_context;
    Handle(V3d_Viewer) m_viewer;
    Handle(Graphic3d_GraphicDriver) m_graphic_driver;
protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);

};

#endif // OCCVIEW_H

二.cpp

#include "occview.h"
#include "ui_occview.h"

OccView::OccView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OccView)
{
    ui->setupUi(this);
    if(m_context.IsNull())//若交互式上下文为空
    {
        //此对象提供与X server的连接,在Windows和Mac OS中不起作用
        Handle(Aspect_DisplayConnection) m_display_connection=new Aspect_DisplayConnection();
       //创建OpenGl图形驱动
        if(m_graphic_driver.IsNull())
        {
            m_graphic_driver=new OpenGl_GraphicDriver(m_display_connection);
        }
        //获取QWidget的窗口系统标识符
        WId window_handle=(WId)winId();
        // 创建Windows NT 窗口
        Handle(WNT_Window) wind=new WNT_Window((Aspect_Handle)window_handle);
       //创建3D查看器
        m_viewer=new V3d_Viewer(m_graphic_driver);
        //创建视图
        m_view=m_viewer->CreateView();
        m_view->SetWindow(wind);
        //打开窗口
        if(!wind->IsMapped())
        {
            wind->Map();
        }
        //创建交互式上下文
        m_context=new AIS_InteractiveContext(m_viewer);
        //设置查看器的光照
        m_viewer->SetDefaultLights();
        //打开背景灯光
        m_viewer->SetLightOn();
        //设置视图的背景颜色为黑色
        m_view->SetBackgroundColor(Quantity_NOC_BLACK);
        m_view->MustBeResized();;
        //显示直角坐标系,可以配置在窗口显示位置、文字颜色、大小、样式
        m_view->TriedronDisplay(Aspect_TOTP_LEFT_LOWER,Quantity_NOC_GOLD,0.08,V3d_ZBUFFER);
       //设置交互式上下文的显示模式
        m_context->SetDisplayMode(AIS_Shaded,Standard_True);
        //设置模型高亮风格
        Handle(Prs3d_Drawer) t_hilight_style = m_context->HighlightStyle();
        t_hilight_style->SetMethod(Aspect_TOHM_COLOR);  // 颜色显示方式
        t_hilight_style->SetColor(Quantity_NOC_LIGHTYELLOW);    // 设置高亮颜色
        t_hilight_style->SetDisplayMode(1); // 整体高亮
        t_hilight_style->SetTransparency(0.2f); // 设置透明度

        // 设置选择模型的风格
        Handle(Prs3d_Drawer) t_select_style = m_context->SelectionStyle();  // 获取选择风格
        t_select_style->SetMethod(Aspect_TOHM_COLOR);  // 颜色显示方式
        t_select_style->SetColor(Quantity_NOC_LIGHTSEAGREEN);   // 设置选择后颜色
        t_select_style->SetDisplayMode(1); // 整体高亮
        t_select_style->SetTransparency(0.4f); // 设置透明度
    }
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);
    setBackgroundRole(QPalette::NoRole);
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);//设置鼠标跟踪
}
void OccView::paintEvent(QPaintEvent *)
{
   m_view->Redraw();
}

void OccView::resizeEvent(QResizeEvent *)
{
   if(!m_view.IsNull())
   {
       m_view->MustBeResized();
   }
}
QPaintEngine *OccView::paintEngine() const
{
    return 0;

}
OccView::~OccView()
{
    delete ui;
}

三:结果显示

问题与解决 

1、出现Qtmainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 "public: __cdecl OccView::OccView(class QWidget *)" (

解决:先把生成的debug文件全部删除,然后点菜单中的构建,然后重新进行构建项目,然后qmake, 之后重新编译。

参考:(​​​​​​(18条消息) mainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 "public: __cdecl about::about(class QWidget *)" (??0abo_qq_39836658的博客-CSDN博客

2、QPaintEngine *paintEngine() const函数必须有要不然结果是这样