将 STL 文件添加到 QT 小部件

add STL file to a QT Widget

我正在尝试使用 QT5.10 和 c++ 创建 STL 文件的查看器。我成功地在 QWindow 中查看了该文件,但我没有找到如何在 QWidget 中查看它。这就是我想要做的 Qt Designer.

这是在 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();

您可以为您的 3D 创建容器 window。此容器是一个 QWidget,您可以将其嵌入布局的其余部分。

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

当然你必须在 3D window 上省略 view.show(); 但在主 window.

上调用它