我有一个自定义 QML 对象Model.qml
来加载和旋转 3D 模型:
模型.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"
}}
在mainwindow.cpp
我setSource
到 qml 中,main.qml
它包含 Model 对象。
主窗口.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);
在drone.cpp 中,我更新属性“rollAngle”以在此属性更改时旋转模型,但无论如何它都不起作用。这是我用来更新“rollAngle”的代码
无人机.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”发生变化,但 3D 模型不旋转。我SequenceAnimation
改用但它也不能运行。谁能给我一些建议?