Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Unable to change the qml property from c++



  • Here I have piece of code to change the qml color property from my c++ source.

     QObject *object =  QObject::findChild <QObject*>("colorBox");
        if(object)
        {
            qDebug("found color_behaviour");
            //object->setProperty("color","white");
            QColor color(Qt::red);
            object->setProperty("color",color);
        }
    

    But, the color property in my qml file is never changed. The above piece of code is inside a function in one of the cpp files. I also even tried the following way from main.cpp file.

            QQmlEngine engine;
            QQmlComponent component(&engine,"qrc:/QML/Components/GTColorBox.qml");
    
            QObject *rootObject = component.create();
    
            QObject *object =  rootObject->findChild <QObject * >("colorBox");
            if(object)
            {
                qDebug("found color_behaviour");
                object->setProperty("color","black");
                QColor color(Qt::red);
                object->setProperty("color",color);
                qDebug("found black color");
            }
    

  • Lifetime Qt Champion

    Hi,

    You should also provide:

    • your QML code.
    • the Qt version you are using
    • the platform you are running on

  • Qt Champions 2018

    In additional informations,
    I try shot code. It's fine in Qt5.12 linux x64.
    Is there a difference in those?

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QColor>
    
    int main(int argc, char *argv[])
    {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
      QGuiApplication app(argc, argv);
    
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/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");
      }
    
      return app.exec();
    }
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Controls 2.2
    
    ApplicationWindow {
      visible: true
      width: 100
      height: 100
      title: qsTr("test")
    
      Rectangle {
        objectName: "colorBox"
    
        width: parent.width
        height: parent.height
        anchors.fill: parent
    
        color: "blue"
      }
    }
    

  • Qt Champions 2017

    What ever u tried will work only if the object is created and displayed. I am not sure about your first sample. Second sample shows your object is created in c++ and not displayed anywhere. I suspect that your qml object is not displayed. So you have nothing to visually check.



  • Unable to visually check, but the debug statements after setProperty line of code are executed successfully, will this link helps in doing visual change? https://stackoverflow.com/questions/51824477/changing-qml-object-value-from-c



  • Yes, the property is succesfully changed but cannot visualize it in the view. Will setting context property help in visual change of the color property in my qml file? In my case, it is not like passing a data from model to qml file but should pass a color change to the qml file. Will this article helps?

    http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html


  • Qt Champions 2017

    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 ?



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

    objectName: "colorBox"
    

  • Qt Champions 2017

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



  • This post is deleted!

  • Qt Champions 2017

    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 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.


  • Qt Champions 2017

    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 The program is crashing during this check


  • Qt Champions 2017

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



  • @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.


  • Qt Champions 2017

    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.



  • 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.



  • 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.


  • Qt Champions 2017

    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.


  • Qt Champions 2018

    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.


  • Qt Champions 2018

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

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



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


Log in to reply