Qt Documentation

One Qt Example

One Qt example demonstrates how to implement a simple QML 3D control that combines the use of three.js library-based Canvas3D rendering with Qt Quick 2D elements. The example shows a view with various benefits of using Qt with related images picked from http://qt.io. The images are displayed on the side of a spinning 3D cube that spins to show the correct image when the tabs at the top of the application are selected. You can also use swipe gestures to spin the cube to navigate between the tabs. The 3D cube control has been implemented as a simple QML type that internaly uses three.js library and Qt Canvas 3D.

Main QML File

In oneqt.qml, we build the 2D content as normal in QML. Then we add a custom ImageCube type into the scene behind the text elements. This custom type, implemented using three.js library, handles the painting of the 3D cube.

Note: The ImageCube 3D UI component can be created and anchored just like any other QML type.

The Custom 3D QML Control

The ImageCube.qml takes six images that it loads and places to the sides of the cube. In addition, the type has a state that defines which of these images are visible and a backgroundColor property that is used when painting the 3D cube. The angleOffset property can be used to adjust the cube's direction when displaying the selected image. In this example the cube component sits on the right edge of the screen so we twist it slightly to the left so that it appears to be facing the rest of the content. This angle is also used by the 3D light so that the light always illuminates the selected face of the cube.

We use RotationAnimation to animate the transition between angles. It enables us to get smooth transitions between different cube orientations and to always rotate the cube along the shortest possible angle distance.

Finally we create the cube mesh from the geometry and material, position it, and add it to the 3D scene.

cube= new THREE.Mesh(geometry, faceMaterial);
scene.add(cube);

Next we create and add some lights to the scene. AmbientLight defines the surrounding light amount and the directional light is positioned so that it highlights the face of the cube that is currently selected.