Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set ui->widget as Qt3DExtras::Qt3DWindow



  • #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <Qt3DCore/QEntity>
    #include <Qt3DExtras/QPhongMaterial>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        Qt3DCore::QEntity *rootEntity;
        Qt3DExtras::QPhongMaterial *material;
    
    private slots:
        void on_actionRender_triggered();
    
    };
    
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <Qt3DExtras/Qt3DWindow>
    #include <Qt3DExtras/QCuboidMesh>
    #include <Qt3DCore/QTransform>
    #include <Qt3DExtras/QForwardRenderer>
    #include <Qt3DRender/QCamera>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        Qt3DExtras::Qt3DWindow *window;
        Qt3DExtras::Qt3DWindow *window = new Qt3DExtras::Qt3DWindow();
        window->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d9f)));
        ui->widget = window; //here
        rootEntity = new Qt3DCore::QEntity;
        window->setRootEntity(rootEntity);
        window->show();
    
        // Material
        material = new Qt3DExtras::QPhongMaterial();
        material->setDiffuse(QColor(QRgb(0xbeb32b)));
    
        Qt3DRender::QCamera *cameraEntity = window->camera();
        cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
        cameraEntity->setPosition(QVector3D(0, 0, 20.0f));
        cameraEntity->setUpVector(QVector3D(0, 1, 0));
        cameraEntity->setViewCenter(QVector3D(0, 0, 0));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionRender_triggered()
    {
        Qt3DCore::QEntity *cuboidEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QCuboidMesh *cuboidMesh = new Qt3DExtras::QCuboidMesh();
        cuboidMesh->setXExtent(3);
        cuboidMesh->setYExtent(2);
        cuboidMesh->setZExtent(4);
        Qt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();
        cuboidTransform->setTranslation(QVector3D(1.0f, -1.0f, 1.0f));
        cuboidTransform->setRotationX(30);
        cuboidTransform->setRotationY(40);
        cuboidEntity->addComponent(cuboidMesh);
        cuboidEntity->addComponent(material);
        cuboidEntity->addComponent(cuboidTransform);
    }
    

Log in to reply