Solved QML Entity Transform doesn't change from C++ Q_PROPERTY
-
Hello,
I implemented the below code in QML:
Entity { id: birdModelID property real x: 0.0 property real y: 0.0 property real z: 0.0 property real scale: 0.018 property real pitch: CPP_CLASS.pitch property real yaw: CPP_CLASS.yaw property real roll: CPP_CLASS.roll Transform { id: transformID translation: Qt.vector3d(birdModelID.x, birdModelID.y, birdModelID.z) rotation: fromEulerAngles(birdModelID.pitch, birdModelID.yaw, birdModelID.roll) scale: birdModelID.scale } Mesh { id: meshbirdID source: "qrc:/3DAssets/bird.obj" } PhongMaterial { id:materialID ambient: "lime" } components: [meshbirdID,transformID, materialID ] }
When roll,pitch,yaw change in C++ (CPP_CLASS) my 3D model doesn't change in QML?
-
hi
@alien said in QML Entity Transform doesn't change from C++ Q_PROPERTY:When roll,pitch,yaw change in C++ (CPP_CLASS) my 3D model doesn't change in QML?
- make sure your Q_PROPERTY has the NOTIFY macro
- make sure, when you change the value of a variable, to emit corresponding <variable name>Changed() signal
-
Dear @lelev,
It has NOTIFY macro also the set function emit corresponding change signal but transform doesn't change my 3D model. -
@alien Can you show use in which way you have declared pitch, yaw, roll and how you have exposed CPP_CLASS to QML?
I should be something like this:
class CppClass : public QObject { Q_OBJECT Q_PROPERTY(qreal pitch READ pitch NOTIFY pitchChanged) Q_PROPERTY(qreal yaw READ yaw NOTIFY yawChanged) Q_PROPERTY(qreal roll READ roll NOTIFY rollChanged) public: explicit CppClass(QObject * parent = nullptr): QObject(parent) {} qreal pitch() const { return m_pitch; } qreal yaw() const { return m_yaw; } qreal roll() const { return m_roll; } void updatePitch(qreal pitch) { if(!qFuzzyCompare(m_pitch, pitch)) { m_pitch = pitch; emit pitchChanged(pitch); } } void updateYaw(qreal yaw) { if(!qFuzzyCompare(m_yaw, yaw)) { m_yaw = yaw; emit pitchChanged(yaw); } } void updateRoll(qreal roll) { if(!qFuzzyCompare(m_roll, roll)) { m_roll = roll; emit rollChanged(roll); } } signals: void pitchChanged(qreal pitch); void yawChanged(qreal yaw); void rollChanged(qreal roll); };
Then export your instance to QML context:
int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; // register instance in QML context CppClass myCppClass; engine.rootContext()->setContextProperty("CPP_CLASS", &myCppClass); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
-
@kromignon thanks for your reply ,does it works for you?
I use the same cpp code except that I don't pass any argument to change signals! Does it matter to pass argument to change signals? -
@alien Signal do not need to have an argument. And yes, this works for me... But I don't use Qt3D.
You can try to add some debug to see if value change arrives QML:
Entity { id: birdModelID property real x: 0.0 property real y: 0.0 property real z: 0.0 property real scale: 0.018 property real pitch: CPP_CLASS.pitch onPitchChanged: console.log("Pitch has changed to " + pitch) property real yaw: CPP_CLASS.yaw onYawChanged: console.log("Yaw has changed to " +yaw) property real roll: CPP_CLASS.roll onRollChanged: console.log("Roll has changed to " + roll) ... }
-
@kromignon Thanks for your reply the problem comes from defining CPP_Class attributes as a float instead of qreal after changing float to qreal it works.
I appreciate your reply and time