Viewing 3D Models Using Qt

In this post I’ll share an example project that describes how to load and view 3D models using Qt (such as Blender files and so on). The source codes here are tested with Qt 5.9.4 which was released a few days ago.

Here’s a  screenshot depicting the application at work:

This project is based on Qt 3D QML classes.  First things’s first you should make sure required modules for Qt 3D are added to your Qt Quick Application project, and your *.pro file, as seen here:

QT += quick qml 3dcore 3drender 3dinput 3dlogic 3dextras 3danimation

Next, you need to add the required import statements to your QML file:

import QtQuick.Scene3D 2.0

import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0

Our QML file will include an ApplicationWindow, with a ToolBar (and a ToolButton) to open a 3D model file using a FileDialog, and a Scene3D that will include the loaded model:

ApplicationWindow
{
	visible: true
	width : 640
	height : 480
	title : qsTr("3D Viewer")

	header : ToolBar
	{
		ToolButton
		{
			text: "Open 3D Model"
			onPressed :
			{
				fileDialog.open()
			}
		}
	}

	FileDialog
	{
		id: fileDialog
		onAccepted :
		{
			sceneLoader.source = fileDialog.fileUrl
		}
	}

	Scene3D
	{
		...
	}
}

Finally, the Scene3D will include a single Enity with all the codes necessary to load and view the model:

Scene3D
{
	anchors.fill: parent

	aspects : ["input", "logic"]
	cameraAspectRatioMode : Scene3D.AutomaticAspectRatio

	Entity
	{
		id: sceneRoot

		Camera
		{
			id: camera
			projectionType : CameraLens.PerspectiveProjection
			fieldOfView : 30
			aspectRatio : 16 / 9
			nearPlane : 0.1
			farPlane : 1000.0
			position : Qt.vector3d(10.0, 0.0, 0.0)
			upVector : Qt.vector3d(0.0, 1.0, 0.0)
			viewCenter : Qt.vector3d(0.0, 0.0, 0.0)
		}

		OrbitCameraController
		{
			camera: camera
		}

		components: [
			RenderSettings
			{
				activeFrameGraph: ForwardRenderer
				{
					clearColor: Qt.rgba(0, 0.5, 1, 1)
					camera : camera
				}
			},
			InputSettings
			{
			}
		]

		Entity
		{
			id: monkeyEntity
			components : [
			SceneLoader
			{
				id: sceneLoader
			}
			]
		}
	}
}

That’s about it. You can download the source codes for this example project using this link. A full list of supported 3D model file formats can be found here. Qt 3D uses Open Asset Import Library (assimp), so ideally it should support the following formats as of the time of writing this post:

3D, 3DS, 3MF, AC, AC3D, ACC, AMJ, ASE, ASK, B3D, BLEND (Blender), BVH, COB, CMS, DAE/Collada, DXF, ENFF, FBX, glTF 1.0 + GLB, glTF 2.0, HMB, IFC-STEP, IRR / IRRMESH, LWO, LWS, LXO, MD2, MD3, MD5, MDC, MDL, MESH / MESH.XML, MOT, MS3D, NDO, NFF, OBJ, OFF, OGEX, PLY, PMX, PRJ, Q3O, Q3S, RAW, SCN, SIB, SMD, STL, STP, TER, UC, VTA, X, X3D, XGL, ZGL

8 Replies to “Viewing 3D Models Using Qt”

  1. Hi Amin, thanks for this post, it was very helpful. Have you experimented with loading models with shaders and materials? For example when I export a model from blender to obj I also get a material file (.mtl) from blender. Is it possible to load the material using the mtl file in Qt? I am also wondering about shaders. For example I have a semi translucent model in blender, how can I import that model into qt 3D scene with its material and shaders so it will look translucent?

    1. Hi Aras,
      I think the best source of information for what you’re looking for is the Open Asset Import Library (or simply Assimp) website below:
      http://www.assimp.org

      Or its GitHub repository below:
      https://github.com/assimp/assimp

      Since that is the underlying library used by Qt anyway. Maybe you can try loading Blender files directly instead of exporting them as OBJ, have you tried that?

  2. Hi amin, very useful post, thanks a lot
    I just wanted to ask you this :
    when i excuted your program on Qt 5.10, it executes but every file I load (no matter the file type), all the meshs are white, and in the Qt creator console it says :
    “No shader program found for DNA”
    multiple times.
    Thank you very much, and keep the great work ! 🙂

    1. Hi Riyad,
      I’d suggest you to try with the most simple files first.
      Just put in a cube or sphere or something and try to debug your way up somehow.
      Unfortunately the message doesn’t seem familiar to me at all.

  3. I am almost their in make this code work. I was able to build or complied assime with Cmake on Qtcreator. Also compiled separately your Viewing 3D. The strange thing is that Assimp build folder their is no bin folder. I can’t link right libraries and include on .pro file. Can you show me the path if I am using qt5.12, Qt Creator 4.8.1, cmake-3.12.4-win64-x64.msi, and assimp 4.1.0. I will Appreciate your help.

    1. Assimp should be already built into Qt, so you don’t need to build it yourself. You can just go ahead and use it.
      Any reasons why you tried to build it yourself?

  4. Thanks for this useful post.
    I am able to compile and run the code you have shared.
    But when I tried to load the 3D .obj file, getting below error:
    Found no suitable importer plugin for QUrl(“file:///C:/Users/Administrator/Documents/LoadObject/cube.obj”)

    Could you give me some solution for this?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.