Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Entity Transform doesn't change from C++ Q_PROPERTY
Forum Updated to NodeBB v4.3 + New Features

QML Entity Transform doesn't change from C++ Q_PROPERTY

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 789 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    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?

    ODБOïO 1 Reply Last reply
    0
    • AlienA Alien

      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?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      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?

      1. make sure your Q_PROPERTY has the NOTIFY macro
      2. make sure, when you change the value of a variable, to emit corresponding <variable name>Changed() signal
      AlienA 1 Reply Last reply
      0
      • ODБOïO ODБOï

        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?

        1. make sure your Q_PROPERTY has the NOTIFY macro
        2. make sure, when you change the value of a variable, to emit corresponding <variable name>Changed() signal
        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by
        #3

        Dear @lelev,
        It has NOTIFY macro also the set function emit corresponding change signal but transform doesn't change my 3D model.

        KroMignonK 1 Reply Last reply
        0
        • AlienA Alien

          Dear @lelev,
          It has NOTIFY macro also the set function emit corresponding change signal but transform doesn't change my 3D model.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @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();
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          AlienA 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @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();
            }
            
            AlienA Offline
            AlienA Offline
            Alien
            wrote on last edited by Alien
            #5

            @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?

            KroMignonK 1 Reply Last reply
            0
            • AlienA Alien

              @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?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @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)
              
              ...
              }
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              AlienA 1 Reply Last reply
              1
              • KroMignonK KroMignon

                @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)
                
                ...
                }
                
                AlienA Offline
                AlienA Offline
                Alien
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved