1. 程式人生 > >Qt例程程式碼分析之basicshapes-cpp

Qt例程程式碼分析之basicshapes-cpp

由於畢業設計設計機器人模擬,但一直想學習Qt。於是,痛下決心,從MFC平臺轉移到Qt上來。現在開始學習如何使用Qt 3D做一個機器人模擬環境出來。分析了一下教程的例程,不是很難,但為了以後查閱方便,所以現在這裡。

下面是例程的執行結果:
例程執行介面
我選擇這個例子是因為許多例程中直接是顯示模型出來的,但沒有提到如何結合按鈕來實現一些互動。這個例子就很好,便於學習。

#include "scenemodifier.h"

#include <QGuiApplication>

#include <Qt3DRender/qcamera.h>
#include <
Qt3DCore/qentity.h> #include <Qt3DRender/qcameralens.h> //介面需要的標頭檔案 #include <QtWidgets/QApplication> #include <QtWidgets/QWidget> #include <QtWidgets/QHBoxLayout> #include <QtWidgets/QCheckBox> #include <QtWidgets/QCommandLinkButton> #include <QtGui/QScreen> //Qt3D標頭檔案,具體作用後續再講
#include <Qt3DInput/QInputAspect> #include <Qt3DExtras/qtorusmesh.h> #include <Qt3DRender/qmesh.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qmaterial.h> #include <Qt3DRender/qeffect.h> #include <Qt3DRender/qtexture.h> #include <Qt3DRender/qrenderpass.
h> #include <Qt3DRender/qsceneloader.h> #include <Qt3DRender/qpointlight.h> #include <Qt3DCore/qtransform.h> #include <Qt3DCore/qaspectengine.h> #include <Qt3DRender/qrenderaspect.h> #include <Qt3DExtras/qforwardrenderer.h> #include <Qt3DExtras/qt3dwindow.h> #include <Qt3DExtras/qfirstpersoncameracontroller.h> int main(int argc, char **argv) { //這句雷打不打要寫... QApplication app(argc, argv); //首先建立一個場景,用於顯示你要繪製或者匯入模型,關鍵語句 Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow(); //設定場景背景顏色 view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f))); //如果將3d放置在介面中,則需要建立一個widget來“裝”它,container這名字起得生動 QWidget *container = QWidget::createWindowContainer(view); //設定場景大小 QSize screenSize = view->screen()->size(); //當你縮小對話方塊時,你希望它最小是多少 container->setMinimumSize(QSize(100, 200)); //當你縮小對話方塊時,你希望它最大是多少 container->setMaximumSize(screenSize); //關鍵語句,建立對話方塊 QWidget *widget = new QWidget; //水平佈局 QHBoxLayout *hLayout = new QHBoxLayout(widget); //垂直佈局 QVBoxLayout *vLayout = new QVBoxLayout(); vLayout->setAlignment(Qt::AlignTop); //將3d場景veiw放到widget中,關鍵語 hLayout->addWidget(container, 1); hLayout->addLayout(vLayout); widget->setWindowTitle(QStringLiteral("Basic shapes")); //不懂 Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect; view->registerAspect(input); //建立根實體,我理解就是建立一個基礎實體,然後用它來建立其他的實體 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); // 建立相機,設定視角 Qt3DRender::QCamera *cameraEntity = view->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)); //建立各種模型實體 Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity); light->setColor("white"); light->setIntensity(1); lightEntity->addComponent(light); Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity); lightTransform->setTranslation(cameraEntity->position()); lightEntity->addComponent(lightTransform); // 設定相機控制,什麼第一視場什麼的,不是很懂,待補充。 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity); camController->setCamera(cameraEntity); //場景修改? SceneModifier *modifier = new SceneModifier(rootEntity); // 關鍵語句,將這麼多實體載入進view當中... view->setRootEntity(rootEntity); //手工碼控制元件 QCommandLinkButton *info = new QCommandLinkButton(); info->setText(QStringLiteral("Qt3D ready-made meshes")); info->setDescription(QString::fromLatin1("Qt3D provides several ready-made meshes, like torus, cylinder, cone, " "cube, plane and sphere.")); info->setIconSize(QSize(0,0)); QCheckBox *torusCB = new QCheckBox(widget); torusCB->setChecked(true); torusCB->setText(QStringLiteral("Torus")); QCheckBox *coneCB = new QCheckBox(widget); coneCB->setChecked(true); coneCB->setText(QStringLiteral("Cone")); QCheckBox *cylinderCB = new QCheckBox(widget); cylinderCB->setChecked(true); cylinderCB->setText(QStringLiteral("Cylinder")); QCheckBox *cuboidCB = new QCheckBox(widget); cuboidCB->setChecked(true); cuboidCB->setText(QStringLiteral("Cuboid")); QCheckBox *planeCB = new QCheckBox(widget); planeCB->setChecked(true); planeCB->setText(QStringLiteral("Plane")); QCheckBox *sphereCB = new QCheckBox(widget); sphereCB->setChecked(true); sphereCB->setText(QStringLiteral("Sphere")); // vLayout->addWidget(info); vLayout->addWidget(torusCB); vLayout->addWidget(coneCB); vLayout->addWidget(cylinderCB); vLayout->addWidget(cuboidCB); vLayout->addWidget(planeCB); vLayout->addWidget(sphereCB); //訊號與槽好基友,根據checkBox的狀態來選擇是否顯示模型 QObject::connect(torusCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableTorus); QObject::connect(coneCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableCone); QObject::connect(cylinderCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableCylinder); QObject::connect(cuboidCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableCuboid); QObject::connect(planeCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enablePlane); QObject::connect(sphereCB, &QCheckBox::stateChanged, modifier, &SceneModifier::enableSphere); //先設定預設情況 torusCB->setChecked(true); coneCB->setChecked(true); cylinderCB->setChecked(true); cuboidCB->setChecked(true); planeCB->setChecked(true); sphereCB->setChecked(true); //顯示視窗 widget->show(); //設定大小 widget->resize(1200, 800); //雷打不動第二句 return app.exec(); }

分析得簡單粗暴,但會繼續研究這個程式碼,並且不斷更新,直到自己對這裡的每一句都非常熟悉!