Qt 3D: Simple C++ Example

A C++ application that demonstrates how to render a scene in Qt 3D.

../_images/simple-cpp.png

Simple demonstrates how to render a scene in Qt 3D.

Running the Example

To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Setting Up the Scene

We set up the scene in the main.cpp file.

To be able to use the classes and functions in the Q3D Core, Q3D Render, Qt 3D Input, and Qt 3D Extras modules, we must include the classes:

First, we set up the scene and specify its root entity:

We specify the material to use for Phong rendering:

The root entity is just an empty shell and its behavior is defined by the components that it references. We specify the torus entity and its mesh, transform, and material components:

We also specify a sphere entity and its components:

We use a property animation to animate the sphere transform.

Finally, we initialize a Qt GUI application that uses a Qt 3D window:

Example project @ code.qt.io