Unsolved Unable to change the qml property from c++
-
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" } }
-
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
-
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"
-
Show me your qml code. Also in the first code sample what happened ? Did it come to if condition ?
-
This post is deleted! -
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.
-
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
-
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.