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. Unable to change the qml property from c++
Forum Updated to NodeBB v4.3 + New Features

Unable to change the qml property from c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
23 Posts 5 Posters 5.2k Views 2 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #7

    There is nothing like passing property to qml file. Qml file is component. Please note what u r changing is object property. So you have to have object displayed.
    Question for you - how are you telling property is not changed ?

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

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

      I have my object being displayed already in my qml file as

      objectName: "colorBox"
      
      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #9

        Show me your qml code. Also in the first code sample what happened ? Did it come to if condition ?

        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
          #10
          This post is deleted!
          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by dheerendra
            #11

            Are you loading this qml in main.cpp ? Can you tell me what is the meaning of the following statement in your code ?
            QObject object = QObject::findChild <QObject>("colorBox");

            For which object you are trying to find a child ?

            @KazuoAsano has already given the sample example. Did you compare with your program ?

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

            J 1 Reply Last reply
            0
            • dheerendraD dheerendra

              Are you loading this qml in main.cpp ? Can you tell me what is the meaning of the following statement in your code ?
              QObject object = QObject::findChild <QObject>("colorBox");

              For which object you are trying to find a child ?

              @KazuoAsano has already given the sample example. Did you compare with your program ?

              J Offline
              J Offline
              JennyAug13
              wrote on last edited by
              #12

              @dheerendra Yes I am loading the qml in my main.cpp but it is in the second case. In the first case iam finding the child from another class in cpp function.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #13

                If you are finding a object, then it should work. Please note you are loading item object. Item does not have color property. So nothing works. In fact setProperty(...) method should fail. Check the return value of setProperty.

                if (!obj->setProperty("color","blue")) {
                    qWarning() << " Failed to set the color Property" <<endl;
                    return;
                }
                

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

                J 1 Reply Last reply
                0
                • dheerendraD dheerendra

                  If you are finding a object, then it should work. Please note you are loading item object. Item does not have color property. So nothing works. In fact setProperty(...) method should fail. Check the return value of setProperty.

                  if (!obj->setProperty("color","blue")) {
                      qWarning() << " Failed to set the color Property" <<endl;
                      return;
                  }
                  
                  J Offline
                  J Offline
                  JennyAug13
                  wrote on last edited by
                  #14

                  @dheerendra The program is crashing during this check

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #15

                    I suspect that obj is null. Please check your program. Please check the sample given by @KazuoAsano & compare your program.

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

                    J 1 Reply Last reply
                    0
                    • dheerendraD dheerendra

                      I suspect that obj is null. Please check your program. Please check the sample given by @KazuoAsano & compare your program.

                      J Offline
                      J Offline
                      JennyAug13
                      wrote on last edited by
                      #16

                      @dheerendra Yes, the object is null. it is not finding the child when i used QQml engine and not QQmlApplication engine from my main.cpp file.

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #17

                        Please show the main.cpp & how are you loading the qml file. Based on this we can suggest. I'm assuming that you are loading the qml which has 'colorBox' object.

                        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
                          #18

                          I tried in this way the same way said by @KazuoAsano, i placed a rectangle in main.qml file and from main.cpp tried to set the property color as red and it remained in blue and here is my code.

                          main.qml file

                          Gui {
                           id: MainView;
                          
                              objectName: "MainView"
                          
                              Rectangle{
                                  objectName: "colorBox"
                                  width: 200
                                  height: 100
                                  anchors.horizontalCenter: parent.horizontalCenter
                                  anchors.verticalCenter: parent.verticalCenter
                                  //anchors.centerIn: parent.Center
                                  color: "blue"
                              }
                              Loader {
                          

                          Here is my main.cpp file

                           QQmlApplicationEngine engine;
                              engine.load(QUrl(QStringLiteral("qrc:/QML/main.qml")));
                              if (engine.rootObjects().isEmpty())
                                  return -1;
                          
                              QObject *rootObject = engine.rootObjects().first();
                              QObject *object = rootObject->findChild<QObject*>("colorBox");
                              if(object)
                              {
                                  QColor color(Qt::red);
                                  object->setProperty("color",color);
                                  qDebug("Change red color");
                              }
                          
                          

                          The colorBox remained in the blue color.

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

                            Yes, setProperty will fail because, it is inside an item. And also when i try to print the object name and children count, it shows correct count for the children and also root object name. The actual children count is displayed as 2 with the

                            children().count();
                            

                            whereas when i try to find the object names

                            using

                            QList<QObject*> list = rootObject->findChildren<QObject *>();
                                qDebug("list = %d",list.count());
                                 int i;
                                 for(i=0;i<list.count();i++)
                                 {
                                    QObject *obj = list[i];
                                    qDebug() << "Object" << i << obj->objectName();
                                 }
                            

                            it displays list count as 81 and among some object names some are empty and object 28 is my colorBox, but unable to set the color property from c++ file.

                            1 Reply Last reply
                            0
                            • dheerendraD Offline
                              dheerendraD Offline
                              dheerendra
                              Qt Champions 2022
                              wrote on last edited by
                              #20

                              So it means that you are have object called a color box. So you should get the reference of this object when you do findChild. If you are getting this you should be able to set the color. When you say it does not work, what do you mean ? Your object is Item. Can you check whether value is set to color. I'm not sure what are you doing with this property in Item after wards.

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

                              1 Reply Last reply
                              1
                              • KazuoAsanoK Offline
                                KazuoAsanoK Offline
                                KazuoAsano
                                Qt Champions 2018
                                wrote on last edited by
                                #21

                                I seem to it is a difficult problem...
                                I wrote code based on this forum thread information,again,

                                https://github.com/sazus/QtForum_topic_98263

                                Can you check the expected behavior of the software?
                                After that you confirm a difference in those and Let's solve this issue with everyone.

                                1 Reply Last reply
                                1
                                • GrecKoG Offline
                                  GrecKoG Offline
                                  GrecKo
                                  Qt Champions 2018
                                  wrote on last edited by
                                  #22

                                  An alternative would be to not do that, it's bad practice and not very maintainable.
                                  Some links to coroborate my claim :

                                  • https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                                  • http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                                  • https://youtu.be/vzs5VPTf4QQ?t=23m20s

                                  A proper alternative would be to set a QObject as a context property and expose properties in it.

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

                                    Yes thankyou, i found a way. But not through qml but tried from two c++ files.

                                    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