How to render in Qt3D in standard GUI application?

前端 未结 2 1758
野的像风
野的像风 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.

    0 讨论(0)
  • 2020-12-28 20:02

    This is how I did it on Qt5.10. This example shows a scene with a cuboid. Scene you can than use like a button or so... To use this add QT += 3dextras to your project file.

    szene.h

    #ifndef SCENE_H
    #define SCENE_H
    
    #include <QObject>
    #include <QWidget>
    
    class Scene
          : public QWidget
    {
        Q_OBJECT
    
    private:
        QWidget *container;
    
    public:
        explicit Scene(QWidget *parent = nullptr);
    
    protected:
        // reimplementation needed to handle resize events
        // http://doc.qt.io/qt-5/qwidget.html#resizeEvent
        void
        resizeEvent ( QResizeEvent * event );
    
    public slots:
        void
        resizeView(QSize size);
    };
    
    #endif // SCENE_H
    

    scene.cpp

    #include "scene.h"
    
    #include <Qt3DExtras/Qt3DWindow>
    #include <Qt3DExtras/QForwardRenderer>
    #include <QQuaternion>
    #include <Qt3DCore/QEntity>
    #include <Qt3DCore/QTransform>
    #include <Qt3DRender/QCamera>
    #include <Qt3DExtras/QCuboidMesh>
    #include <Qt3DExtras/QPhongMaterial>
    
    Scene::Scene(QWidget *parent)
       : QWidget(parent)
    {
        auto view = new Qt3DExtras::Qt3DWindow();
    
        // create a container for Qt3DWindow
        container = createWindowContainer(view,this);
    
        // background color
        view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x575757)));
    
        // Root entity
        auto rootEntity = new Qt3DCore::QEntity();
    
        // Camera
        auto camera = new Camera(rootEntity,view);
        auto cameraEntity = view->camera();
    
        cameraEntity->setPosition(QVector3D(0, 0, 50.0f));
        cameraEntity->setUpVector(QVector3D(0, 1, 0));
        cameraEntity->setViewCenter(QVector3D(0, 0, 0));
    
        // Cuboid
        auto cuboidMesh = new Qt3DExtras::QCuboidMesh();
    
        // CuboidMesh Transform
        auto cuboidTransform = new Qt3DCore::QTransform();
        cuboidTransform->setScale(10.0f);
        cuboidTransform->setTranslation(QVector3D(0.0f, 0.0f, 0.0f));
        cuboidTransform->setRotation(QQuaternion(1,1.5,1,0).normalized());
    
        auto cuboidMaterial = new Qt3DExtras::QPhongMaterial();
        cuboidMaterial->setDiffuse(QColor(QRgb(0x005FFF)));
    
        // assamble entity
        auto cuboidEntity = new Qt3DCore::QEntity(rootEntity);
        cuboidEntity->addComponent(cuboidMesh);
        cuboidEntity->addComponent(cuboidMaterial);
        cuboidEntity->addComponent(cuboidTransform);
    
        // Set root object of the scene
        view->setRootEntity(rootEntity);
    }
    
    void
    Scene::resizeView(QSize size)
    {
        container->resize(size);
    }
    
    void
    Scene::resizeEvent ( QResizeEvent * /*event*/ )
    {
      resizeView(this->size());
    }
    
    0 讨论(0)
提交回复
热议问题