简体   繁体   中英

add STL file to a QT Widget

I'm trying to create a viewer for STL files with QT5.10 and c++. I suceeded to view the file in a QWindow but I didn't find how to view it in a QWidget . This is what i want to do Qt Designer .

This is the code for viewing it in a QWindow :

Qt3DExtras::Qt3DWindow view;

Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);

Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
material->setDiffuse(QColor(200, 200, 200));

Qt3DRender::QMesh *stlObjectMesh = new Qt3DRender::QMesh;
stlObjectMesh->setMeshName("Onshape");
stlObjectMesh->setSource(data);
flyingwedge->addComponent(stlObjectMesh);
flyingwedge->addComponent(material);

Qt3DRender::QCamera *camera = view.camera();
camera->lens()->setPerspectiveProjection(1000.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(-500, -100, 40.0f));
camera->setViewCenter(QVector3D(0, 0, 0));

Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(0.8f);
lightEntity->addComponent(light);

Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
lightEntity->addComponent(lightTransform);

Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);

camController->setCamera(camera);
camController->setLinearSpeed( 5000.0f );
camController->setLookSpeed( 1000.0f );
view.setRootEntity(rootEntity);
view.show();

You can create a container for your 3D window. This container is a QWidget which you can then embed in the rest of your layout.

QWidget *container = QWidget::createWindowContainer(3DWindow);
widgetLayout->addWidget(container);

Of course you have to omit view.show(); on the 3D window but call this on your main window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM