How to render in Qt3D in standard GUI application?

前端 未结 2 1792
野的像风
野的像风 2020-12-28 19:06

I enjoy using Qt3D, but all of the examples I see for it are full window applications. What I can\'t understand from the examples is how to add a qt3d rendering window to a

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 19:41

    You can subclass QGLView class which extends QGLWidget with support for 3D viewing:

    class GLView : public QGLView
    {
        Q_OBJECT
    
    public:
        GLView(QWidget *parent = 0);
        ~GLView();
    
    protected:
        void initializeGL(QGLPainter *painter);
        void paintGL(QGLPainter *painter);
    
    private:
        QGLAbstractScene *m_scene;
        QGLSceneNode *m_rootNode;
    };
    
    GLView::GLView(QWidget *parent)
        : QGLView(parent)
        , m_scene(0)
        , m_rootNode(0)
    {
        // Viewing Volume
        camera()->setFieldOfView(25);
        camera()->setNearPlane(1);
        camera()->setFarPlane(1000);
    
        // Position of the camera
        camera()->setEye(QVector3D(0, 3, 4));
    
        // Direction that the camera is pointing
        camera()->setCenter(QVector3D(0, 3, 0));
    }
    
    GLView::~GLView()
    {
        delete m_scene;
    }
    
    void GLView::initializeGL(QGLPainter *painter)
    {
        // Background color
        painter->setClearColor(QColor(70, 70, 70));
    
        // Load the 3d model from the file
        m_scene = QGLAbstractScene::loadScene("models/model1/simplemodel.obj");
    
        m_rootNode = m_scene->mainNode();
    }
    
    void GLView::paintGL(QGLPainter *painter)
    {
        m_rootNode->draw(painter);
    }
    

    Qt 5.1 introduces the function QWidget::createWindowContainer(). A function that creates a QWidget wrapper for an existing QWindow, allowing it to live inside a QWidget-based application. You can use QWidget::createWindowContainer which creates a QWindow in a QWidget. This allows placing QWindow-subclasses in Widget-Layouts. This way you can embed your QGLView inside a widget.

提交回复
热议问题