Nuno Bártolo
Nuno Bártolo

Reputation: 75

Render a Qt3d view as a widget on QApplication MainWindow

I'm starting out in Qt. I'm trying to render a 3D view from Qt3d on the MainWindow of a QApplication.

The examples I've found are all based on QGuiApplications without widgets using Qt Quick and QML but I'm aiming to use widgets since this is going to be a desktop application.

I have used Qt designer for my MainWindow UI form, On the central widget I have used Vertical layout to place tabs on my left and an empty widget with expansive horizontal size policy on the right. My goal is to create a Class that is based on an existing Qt class that will support Qt3d while also being able to handle widgets. Then from the Mainwindow form I want to promote the Widget to that class.

I've tried QWindow but it doesn't allow widgets, am I suppose to Use QGlview to get access to the widgets?I'm not really looking to mess with opengl and isn't that deprecated now? Can someone direct me to an example that implements this?

Thank you

Upvotes: 5

Views: 4843

Answers (3)

Florian Blume
Florian Blume

Reputation: 3345

If anyone stumbles across this question and is looking for a solution without Qt3DWindow and createWindowContainer, I implemented a Qt3DWidget here. It renders to an offscreen texture and uses this texture on a quad in a QOpenGLWidget. Minimal interference and overhead.

I needed such a widget because I have a program where you can drag the 3D view around and createWindowContainer draws the contained child always on top of everything.

Upvotes: 0

Bill Sellers
Bill Sellers

Reputation: 466

If you want to use a QWidget then the easiest way is to create a Qt3DWindow and convert it into a QWidget using createWindowContainer. You can use the returned QWidget like any other (add it to layouts or whatever).

Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
QWidget *container = QWidget::createWindowContainer(view);

The only problem with this approach is that it isn't quite so straightforward to use Qt Designer. I tend to just use a placeholder QWidget in Qt Designer and then use code like the above in the MainWindow constructor to create the required QWidget and either replace my placeholder or put this new widget inside the placeholder widget

Upvotes: 8

marv
marv

Reputation: 1

The QML item you are looking for is called Scene3D:

http://doc.qt.io/qt-5/qt3d-scene3d-example.html

import QtQuick.Scene3D 2.0

Scene3D {
    id: scene3d
    anchors.fill: parent
    Entity {
        Camera {
            id: camera
            projectionType: CameraLens.PerspectiveProjection
            fieldOfView: 45
            aspectRatio: scene3d.width/scene3d.height
            nearPlane : 0.1
            farPlane : 1000.0
            position: Qt.vector3d( 0.0, 0.0, -40.0 )
            upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
            viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
        }
        Entity {
            components: [
                SphereMesh {},
                PhongMaterial {}
            ]
        }
        components: [
            FrameGraph {
                activeFrameGraph: Viewport {
                    rect: Qt.rect(0.0, 0.0, 1.0, 1.0)
                    clearColor: "transparent"
                    CameraSelector {
                        camera: camera
                        ClearBuffer {
                            buffers : ClearBuffer.ColorDepthBuffer
                        }
                    }
                }
            }
        ]
    }
}

Upvotes: -2

Related Questions