In c++ side I wrote this code
:::::::::::::
QMetaObject::invokeMethod(rootObject,"changeText",Q_ARG(QVariant,"txt1"),
Q_ARG(QVariant,"hello"))
in qml side I wrote this
Text {
id: txt1
text: "hi"
}
function changeText(id,str){
id.text=str
}
changeText function works in qml side but it doesn’t work when I call it from c++ side. I think Cpp side method sends “txt1” as QString so changeText function doesn’t work.
Can you tell me please how can I do this?
The correct way of changing properties of qml objects from c++ is to get that object in c++ and than call setProperty() method. Example:
qml:
Note that you have to add a object name property that is used to get the child. In this example the Rectangle is the rootObject. Then in c++:
You can compact this to one line call like this:
An alternative is to use the approach you had before but give object name to changeText method and iterate through the children of the main object until you find the one you are interested in: