[Interest] [Qt3D] Mixing C++ and QML

Xavier Bigand flamaros.xavier at gmail.com
Sun Oct 22 00:15:32 CEST 2017


Hello Sean,

I don't really understand how to use
QAbstractNodeFactory::createNode<QEntity>("QEntity")
and it seems to be a part of private API which I prefer to avoid.

But I found a simpler way, I have created a Class that inherit QEntity and
in the constructor I simply set the it as parent of the other C++ QEntity
that is the root of my elements.
I am not really confortable to attach elements by the parent, I used to do
it with children (but in Qt3D API we can't modify the children list).

*C++*

    SceneEntity::SceneEntity(QNode* parent)

        : QEntity(parent)

    {

        if (Scene::singleton()->rootEntity() == nullptr)

            Scene::singleton()->initialize();


        Scene::singleton()->rootEntity()->setParent(this);

    }


*QML*

Scene3D {

    id: scene3D


    focus: true

    aspects: ["input", "logic"]

    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio


    Entity {

        Camera {

            id: camera

            projectionType: CameraLens.PerspectiveProjection

            fieldOfView: 70.0

            nearPlane : 0.2

            farPlane : 100.0

            position: Qt.vector3d(2.0, -10.0, 10.)

            upVector: Qt.vector3d(0.0, 0.0, 1.0)

            viewCenter: Qt.vector3d(0.0, 0.0, 0.0)

        }


        OrbitCameraController {

            camera: camera

            zoomInLimit: 2.0

            linearSpeed: 50.0

            lookSpeed: 180.0

        }


        components: [

            RenderSettings {

                activeFrameGraph: ForwardRenderer {

                    camera: camera

                    clearColor: Qt.rgba(64 / 255, 128 / 255, 128 /
255, 255 / 255)

                }

            },

            InputSettings { }

        ]


        GeoScene { // Attach the entities tree

        }

}


But now as objects can have only one parent, I am asking myself how I
can render the same scene in different items. I want to have the same
scene with different cameras and rendering effects in distinct docks
or views.

May I have some other Scene3D that use camera of an other one?


Here is a capture of what I have actually:

http://hpics.li/75ae344

I want to see the same scene (colored axis) in two other scenes.



Maybe the solution is to render all viewports in one scene off screen, then
display them separately after, but in this way it will be complicated to
manage inputs for the camera.


2017-10-19 18:06 GMT+02:00 Sean Harmer <sean.harmer at kdab.com>:

> Hi,
>
> you've hit one of the issues we did with exposing C++ and QML APIs.
> Namely, QQmlListProperty. This thing should ideally never have existed and
> instead support for properties of collections should have been added to
> QObject and the metaobject system.
>
> To avoid pulling in a QtQml dependency in the C++ API, we instead have
> another layer to add the QML specifics (pretty much entirely list
> properties).
>
> In order to create these from code we have a factory. Try something like:
>
> QAbstractNodeFactory::createNode<QEntity>("QEntity")
>
> If using just C++ you will get a standard QEntity. If using QML (and the
> types are registered with the factory) you will get the QML -enhanced
> version with the extension object that handles the list properties.
>
> We use this for e.g. in the renderer's SkeletonLoader class to create
> QJoints for the frontend tree.
>
> Cheers,
>
> Sean
>
> On 19/10/2017 16:40, Xavier Bigand wrote:
>
>> The issue is that QEntity doesn't reflect directly the qml type Entity,
>> this is the same for many Qt3D classes.
>>
>> Here is the declaration of QEntity from qentity.h
>>
>> class QT3DCORESHARED_EXPORT QEntity : public QNode
>>
>> {
>>
>> Q_OBJECT
>>
>> public:
>>
>> explicit QEntity(QNode *parent = nullptr);
>>
>> virtual ~QEntity();
>>
>>
>> QComponentVector components() const;
>>
>>
>> void addComponent(QComponent *comp);
>>
>> void removeComponent(QComponent *comp);
>>
>>
>> QEntity *parentEntity() const;
>>
>>
>> protected:
>>
>> explicit QEntity(QEntityPrivate &dd, QNode *parent = nullptr);
>>
>>
>> private:
>>
>> Q_DECLARE_PRIVATE(QEntity)
>>
>>
>> QNodeCreatedChangeBasePtr createNodeCreationChange() const
>> Q_DECL_OVERRIDE;
>>
>> };
>>
>>
>>
>> As you can see the method addComponent is not virtual and exported as
>> Q_PROPERTY, so when creating a derived class of QEntity when instanced in
>> qml it doesn't inherited of Entity properties.
>>
>>
>>
>>
>> 2017-10-19 15:44 GMT+02:00 Jason H <jhihn at gmx.com <mailto:jhihn at gmx.com
>> >>:
>>
>>     > I am looking for a way to manage an Entity tree in C++ and the
>> Scene configuration in QML. My C++ code manage entities from a root node
>> and I want to move the Frame graph and camera management,... to qml.
>>     > I can't figure out how to do that because if create a class that
>> derive from QEntity some properties of Entity aren't declared like
>> "components".
>>     >
>>     > My goal is to have an object hierarchy made with QEntity in c++ and
>> dynamic views on it made in QML. Scene views should be dockable,...
>>
>>
>>     This is well-documented. QEntity inherits from QObject so you are
>>     good. I'm not sure what you mean by "like components". Public
>>     properties, signals and slots are visible to QML. If not it's a
>>     simple matter of wrapping them in a public one.
>>
>>     http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
>>     <http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html>
>>
>>
>>
>>
>>
>>
>> --
>> Xavier
>>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
> --
> Dr Sean Harmer | sean.harmer at kdab.com | Managing Director UK
> KDAB (UK) Ltd, a KDAB Group company
> Tel. +44 (0)1625 809908; Sweden (HQ) +46-563-540090
> Mobile: +44 (0)7545 140604
> KDAB - Qt Experts
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>



-- 
Xavier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171022/e4ccd5c9/attachment.html>


More information about the Interest mailing list