How to render in Qt3D in standard GUI application?

前端 未结 2 1800
野的像风
野的像风 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 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 
    #include 
    
    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 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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());
    }
    

提交回复
热议问题