之前项目工作中需要实现一个功能:在控制软件中通过3D的方式显示我们的设备,使其能够更随设备位置的变化而变化。由于控制软件我使用的Qt进行的开发,那么自然就可以使用Qt3D来实现这个功能。我使用的开发环境为Qt Creator,Qt版本为Qt5.10.1。
首先看看Qt官方有没有什么合适的例子吧,在Qt的示例代码中搜索“3D”,就可以看到这样一个工程"Qt 3D: Basic Shapes C++ Example "。这是一个非常好的例子,通过这个例子我们可以对Qt3D有个大致的了解。比如:如果要支持3D显示,需要在pro文件中添加哪些东西,需要包含哪些头文件,以及显示一个3D模型的基本过程。
首先是pro文件中需要添加对3D显示的支持 QT += 3dcore 3drender 3dinput 3dextras
然后就可以添加对应的头文件并添加和显示3D图形了。下面的代码是我摘抄Basic Shapes C++ Example中部分的代码,并加上了注释
#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>
#inc