notthing
notthing

Reputation: 31

Qt: Rotate 3D Model

I had a custom QML object Model.qml to load and rotate 3D models:

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"
}}

In mainwindow.cpp I setSource to qml, and main.qml it contains Model object.

mainwindow.cpp

QWidget *container = QWidget::createWindowContainer(&view);
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setDepthBufferSize(24);
view.setFormat(format);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:/src/main.qml"));
ui->scrollArea_3D->setWidget(container);

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");        
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 advices?

Upvotes: 3

Views: 2447

Answers (1)

oria66
oria66

Reputation: 123

This post is old, but I can share my own personal experience with an example. If other people search for the answer there it is.

I don't know your project, but if you want to modifiy the angle of rotation from C++ use a private variable of MainWindow that sends the value with a QTimer with a Q_PROPERTY macro.

If you want only use QML, you can see this github project. It's very simple. https://github.com/oria66/test-qml-3d-model-rotation

Upvotes: 2

Related Questions