PDA

View Full Version : Qt 5.5: Rotate 3D Model



notthing
28th July 2015, 10:45
I had a custom QML object "Model.qml" to load and rotate 3D model.

Model.qml
Entity {
id: root

property Material material

property alias myRoll : transform.rollAngle

components: [ transform, mesh, root.material ]

Transform {

id: transform
objectName: "MyModel"

property real rollAngle : 0
property real pitchAngle : 20

Translate { id: translation }
Scale { id: scaleTransform }

Rotate {
objectName: "rotateRoll"
axis : Qt.vector3d(1, 0, 0)
angle : transform.rollAngle
}

}


Mesh {
id: mesh
source: "qrc:/3dmodel/Drone.obj"
}
}

And in drone.cpp i update property "rollAngle" to rotate model whenever this property changed but it doesn't work anyway. Here is the code I use to update "rollAngle"

drone.cpp
QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:/src/Model.qml"));
QObject *object = component.create();
QObject *rotateObject = object->findChild<QObject *>("rotateRoll");
childObject->setProperty("rollAngle",this->roll);
rotateObject->setProperty("angle", this->roll);
qDebug() << "Property value:" << rotateObject->property("angle").toFloat();
engine.destroyed();

"rollAngle" changes but 3D model doesn't rotate. I use SequenceAnimation instead but it can't run too. Can anyone give me some advice?

Thks.

anda_skoa
28th July 2015, 14:45
Better than calling into QML is to expose the values via properties and then bind them in QML.

I.e. add whatever values you want to change from C++ side as Q_PROPERTY to the drone object, then set the object as a context property.
The object becomes available in QML and its properties with it. The properties can then be used just like properties of other QML elements.

Cheers,
_

notthing
29th July 2015, 04:46
First thks anda_skoa :)

But I misunderstand "set the object as a context property" can you explain in more detail or can you give a simple example?

Thks again.

anda_skoa
29th July 2015, 10:12
The QQmlEngine has a root context (of type QQmlContext) and it can be used to "export" values and objects from C++ to be used from QML.

See QQmlContest::setContextProperty().

When an object (an instance of a QObject derived class) is exported like this, its properties (defined via Q_PROPERTY) become as easily usable in QML as properties of QML elements.
I.e. you can create bindings that include them and those bindings get reevaluated automatically if the property value changes.

See http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html for an example

Cheers,
_

notthing
29th July 2015, 11:31
Ok, I'll try this way. Thks.

notthing
4th August 2015, 05:58
The QQmlEngine has a root context (of type QQmlContext) and it can be used to "export" values and objects from C++ to be used from QML.

See QQmlContest::setContextProperty().

When an object (an instance of a QObject derived class) is exported like this, its properties (defined via Q_PROPERTY) become as easily usable in QML as properties of QML elements.
I.e. you can create bindings that include them and those bindings get reevaluated automatically if the property value changes.

See http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html for an example

Cheers,
_

Thks a lot anda_skoa

I solved this issue :) Just create Q_PROPERTY attribute roll in Drone.h that links to attribute roll of object Model.qml and set rootContext to Drone. Then Q_EMIT the function notify with roll (in Drone.h) and the Rotate object in Model.qml we change to drone.roll. And finally it works.