Qt3D + QMainWindow + Qt 5.5

0

Witam!

Usiłuję dodać elementy 3d do istniejącej aplikacji. Problem jest, by grafika 3d renderowała się w oknie programu (nie w nowym).
Przykładowy przykład:

#include "mainwindow.h"
#include "ui_mainwindow.h"

//QT       += core gui 3dcore 3drenderer 3dinput
#include <Qt3DCore>
#include <Qt3DRenderer>
#include <Qt3DInput>
// verticalLayout na mainwindow.ui

//#define CHCE_CIE_W_WIDGECIE_UJU

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Qt3D::QWindow * view = new Qt3D::QWindow();

    // Root entity
    Qt3D::QEntity *rootEntity = new Qt3D::QEntity();

    // Camera
    Qt3D::QCamera *cameraEntity = view->defaultCamera();

    cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 0, -40.0f));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));
    //input->setCamera(cameraEntity);

    // Material
    Qt3D::QMaterial *material = new Qt3D::QPhongMaterial(rootEntity);

    // Torus
    Qt3D::QEntity *torusEntity = new Qt3D::QEntity(rootEntity);
    Qt3D::QTorusMesh *torusMesh = new Qt3D::QTorusMesh;
    torusMesh->setRadius(5);
    torusMesh->setMinorRadius(1);
    torusMesh->setRings(100);
    torusMesh->setSlices(20);

    Qt3D::QTransform *torusTransform = new Qt3D::QTransform;
    Qt3D::QScaleTransform *torusScaleTransform = new Qt3D::QScaleTransform;
    torusScaleTransform->setScale3D(QVector3D(1.5, 1, 0.5));

    Qt3D::QRotateTransform *torusRotateTransform = new Qt3D::QRotateTransform;
    torusRotateTransform->setAxis(QVector3D(1, 0, 0));
    torusRotateTransform->setAngleDeg(45);

    torusTransform->addTransform(torusScaleTransform);
    torusTransform->addTransform(torusRotateTransform);

    torusEntity->addComponent(torusMesh);
    torusEntity->addComponent(torusTransform);
    torusEntity->addComponent(material);

    // Sphere
    Qt3D::QEntity *sphereEntity = new Qt3D::QEntity(rootEntity);
    Qt3D::QSphereMesh *sphereMesh = new Qt3D::QSphereMesh;
    sphereMesh->setRadius(3);

    Qt3D::QTransform *sphereTransform = new Qt3D::QTransform;
    Qt3D::QTranslateTransform *sphereTranslateTransform = new Qt3D::QTranslateTransform;
    sphereTranslateTransform->setTranslation(QVector3D(20, 0, 0));

    Qt3D::QRotateTransform *sphereRotateTransform = new Qt3D::QRotateTransform;
    QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereRotateTransform);
    sphereRotateTransformAnimation->setTargetObject(sphereRotateTransform);
    sphereRotateTransformAnimation->setPropertyName("angle");
    sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0));
    sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360));
    sphereRotateTransformAnimation->setDuration(10000);
    sphereRotateTransformAnimation->setLoopCount(-1);
    sphereRotateTransformAnimation->start();

    sphereRotateTransform->setAxis(QVector3D(0, 1, 0));
    sphereRotateTransform->setAngleDeg(0);

    sphereTransform->addTransform(sphereTranslateTransform);
    sphereTransform->addTransform(sphereRotateTransform);

    sphereEntity->addComponent(sphereMesh);
    sphereEntity->addComponent(sphereTransform);
    sphereEntity->addComponent(material);

    view->setRootEntity(rootEntity);

#ifdef CHCE_CIE_W_WIDGECIE_UJU
    QWidget *container = QWidget::createWindowContainer(view);
    container->setMinimumSize(50, 50);
    container->setMaximumSize(5000, 5000);
    container->setFocusPolicy(Qt::TabFocus);
    ui->verticalLayout->addWidget(container);
#else
    view->show();
#endif
}

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

// plik ten zawiera wszystkie istotne informacji do odtworzenia problemu

Jest to kod który przekleiłem z przykładów i na podstawie obszernej wiedzy googla usiłuję wcisnąć w verticalLayout'a znajdującego się w głównym oknie aplikacji.

Jeśli define nie jest zakomentowany, tzn obecny:

#define CHCE_CIE_W_WIDGECIE_UJU

Animacja powinna wyświetlić się w oknie, lecz zostaje "przeźroczyste", oraz przy zamknięciu:

Qt3D.Core.Nodes: void Qt3D::QEntity::addComponent(Qt3D::QComponent*) Qt3D::QCameraLens(0x144edf0)
Qt3D.Core.Nodes: void Qt3D::QEntity::addComponent(Qt3D::QComponent*) Qt3D::QTransform(0x1410640)
QWaitCondition: cv destroy failure: Urządzenie lub zasoby zajęte
QWaitCondition: mutex destroy failure: Urządzenie lub zasoby zajęte
QThread: Destroyed while thread is still running

I że tak napiszę:
poomocyy! ;)

0

Tak będzie lepiej:

   /* ... */
    view->setRootEntity(rootEntity);
 
#ifdef CHCE_CIE_W_WIDGECIE_UJU
    QWidget *container = QWidget::createWindowContainer(view);
    container->setMinimumSize(50, 50);
    container->setMaximumSize(5000, 5000);
    container->setFocusPolicy(Qt::TabFocus);
    ui->verticalLayout->addWidget(container);
#endif
    view->show();
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1