Unsolved Unable to change the qml property from c++
-
@dheerendra The program is crashing during this check
-
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.
-
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.
-
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.
-
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. -
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.
-
Yes thankyou, i found a way. But not through qml but tried from two c++ files.