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. Changing property in qml file from c++ file
Qt 6.11 is out! See what's new in the release blog

Changing property in qml file from c++ file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
14 Posts 3 Posters 2.0k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    JennyAug13
    wrote on last edited by
    #3

    Oh I am a beginner in this field and how can i do it in right way? I want to make the window width to 300. Thanks in advance.

    raven-worxR 1 Reply Last reply
    0
    • J JennyAug13

      Oh I am a beginner in this field and how can i do it in right way? I want to make the window width to 300. Thanks in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #4

      @JennyAug13
      one possiblity:

      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      if (engine.rootObjects().isEmpty())
          return -1;
      QQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300));  // or use setProperty()
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JennyAug13
        wrote on last edited by
        #5

        Yes, it worked like above said way.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JennyAug13
          wrote on last edited by
          #6

          How can i use setProperty() and do it?

          raven-worxR 1 Reply Last reply
          0
          • J JennyAug13

            How can i use setProperty() and do it?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #7

            @JennyAug13
            replace

            QQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300));
            

            with

            engine.rootObjects().first()->setProperty( "width", QVariant::fromValue<qreal>(300) );
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            J 1 Reply Last reply
            3
            • raven-worxR raven-worx

              @JennyAug13
              replace

              QQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300));
              

              with

              engine.rootObjects().first()->setProperty( "width", QVariant::fromValue<qreal>(300) );
              
              J Offline
              J Offline
              JennyAug13
              wrote on last edited by
              #8

              @raven-worx In the similar way i have a c++ file for setting the pulse width and as the pulse width is changed in the c++ file, corresponding blur radius of the button has to be changed. How can I do it? It is not having engine.rootObjects and it is not my main.cpp file.

              Here is the code i hav written but I can see nothing has been done.

              QQuickView view;
                 view.setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTActionButton.qml")));
                 view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3);
                 qCDebug(LC_NTUserSettingsModel())<< "pulseWidth is changed"<<"button radius is changed";
              

              But i can see there is no change in the blur radius of the button. It would be great if someone helps me in finding right way to change my qml property from c++ file, thanks in advance.

              raven-worxR 1 Reply Last reply
              0
              • J JennyAug13

                @raven-worx In the similar way i have a c++ file for setting the pulse width and as the pulse width is changed in the c++ file, corresponding blur radius of the button has to be changed. How can I do it? It is not having engine.rootObjects and it is not my main.cpp file.

                Here is the code i hav written but I can see nothing has been done.

                QQuickView view;
                   view.setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTActionButton.qml")));
                   view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3);
                   qCDebug(LC_NTUserSettingsModel())<< "pulseWidth is changed"<<"button radius is changed";
                

                But i can see there is no change in the blur radius of the button. It would be great if someone helps me in finding right way to change my qml property from c++ file, thanks in advance.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #9

                @JennyAug13 said in Changing property in qml file from c++ file:

                view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3);

                setContextProperty is the incorrect method here. A property name such as actionButton.shadowBlurRadius is also not possible.

                You need an instance to actionButton and call setProperty() on it (just like you did before).

                One way can be to expose the object via a property:

                Window {
                    id: rootId
                
                   property alias actionButton: actionButtonId
                
                  Item {
                     id: actionButtonId
                     ...
                 }
                

                Then you can do

                engine.rootObjects().first()->property("actionButton").value<QQuickItem*>()->setProperty("shadowBlurRadius", 3);
                

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JennyAug13
                  wrote on last edited by
                  #10

                  I tried like in the following way, since I am trying to modify my qml file from another c++ source file unlike from the main file. It works fine but it is showing following message

                  void NTUserSettingsModel::adjustIntensityForPulseWidth()
                  {
                      qDebug("@@@");
                      QQmlEngine engine;
                      qDebug("@@@");
                      QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml")));
                      qDebug("@@@");
                      QObject *object = component.create();
                      qDebug("@@@");
                      object->setProperty("currentIntensityAAA",QVariant(0));
                      qDebug("@@@");
                  }
                  
                  

                  And my qml file has following changes:

                  Component.onCompleted: {
                          currentIntensity = currentIntensityAAA
                          console.log("@@@@@@")
                          console.log(currentIntensity)
                          console.log("@@@@@@")
                      }
                  

                  The error which has been shown when i change my pulseWidth is

                  [default]: qrc:/QML/Components/NTIntensityControl.qml:188: ReferenceError: currentIntensityAAA is not defined
                  

                  Should i pass the value to my currentIntensityAAA as global in my cpp source file?

                  raven-worxR 1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #11

                    Swap the statements. It should work.

                    object->setProperty("currentIntensityAAA",QVariant(0));
                    QObject *object = component.create();
                    

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JennyAug13
                      wrote on last edited by JennyAug13
                      #12

                      When i swapped it shows an error called use of undeclared identifier 'object' beside ```
                      object->setProperty("currentIntensity",QVariant(0));

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JennyAug13
                        wrote on last edited by
                        #13

                        When i try the other way, the program unexpectedly finishes.

                        view->rootContext()->setContextProperty(QStringLiteral("currentIntensityAAA"),QVariant(0));
                        view->setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml")));
                        
                        1 Reply Last reply
                        0
                        • J JennyAug13

                          I tried like in the following way, since I am trying to modify my qml file from another c++ source file unlike from the main file. It works fine but it is showing following message

                          void NTUserSettingsModel::adjustIntensityForPulseWidth()
                          {
                              qDebug("@@@");
                              QQmlEngine engine;
                              qDebug("@@@");
                              QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml")));
                              qDebug("@@@");
                              QObject *object = component.create();
                              qDebug("@@@");
                              object->setProperty("currentIntensityAAA",QVariant(0));
                              qDebug("@@@");
                          }
                          
                          

                          And my qml file has following changes:

                          Component.onCompleted: {
                                  currentIntensity = currentIntensityAAA
                                  console.log("@@@@@@")
                                  console.log(currentIntensity)
                                  console.log("@@@@@@")
                              }
                          

                          The error which has been shown when i change my pulseWidth is

                          [default]: qrc:/QML/Components/NTIntensityControl.qml:188: ReferenceError: currentIntensityAAA is not defined
                          

                          Should i pass the value to my currentIntensityAAA as global in my cpp source file?

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by raven-worx
                          #14

                          @JennyAug13 said in Changing property in qml file from c++ file:

                          void NTUserSettingsModel::adjustIntensityForPulseWidth()
                          {
                          qDebug("@@@");
                          QQmlEngine engine;
                          qDebug("@@@");
                          QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml")));
                          qDebug("@@@");
                          QObject *object = component.create();
                          qDebug("@@@");
                          object->setProperty("currentIntensityAAA",QVariant(0));
                          qDebug("@@@");
                          }

                          i am not sure if you quite understand some C++ basics yet.
                          Again - like already mentioned before - you are creating a QQmlEngine on the stack => the QQmlEngine instance will be deleted once you exit the method. Also you are not supposed to create objects from components just when you want to set a property of a specific object.
                          Thus everything you do in this method might do something, but only in a temporary UI.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          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