Getting root object from c++
Hello freinds,
I have a question. Say I have this
Code:
....
Rectangle{
id::root
property string mystring:""
MyPlugin{
Component.onCompleted: {
setString("Hello");
}
}
}
How can i access my string mystring from my plugins method say componentComplete(), which is an implementation from QQmlParserStatus, or from Q_INVOKABLE MyPlugin::setString Q_INVOKABLE ???
Re: Getting root object from c++
Seems to be a very stupid question from me?!?!?!
Re: Getting root object from c++
Quote:
Seems to be a very stupid question from me?!?!?!
Well, lets say no one will argue with the above statement ;-)
Its all in the docs:
http://doc.qt.io/qt-5/qtqml-cppinteg...mlfromcpp.html
(see Accessing Members of a QML Object Type from C++)
Re: Getting root object from c++
Well,
first of all thank you for reply me. I read the docs. But I dont find the place where it describes
accessing some property of the whatever.qml file from MyPlugin{}, which is loaded from qml.exe by the way.
The docs describes accessing it from main.cpp where I load the whatever.qml in fix manner. I dont provide a main.cpp. Thats my problem.
Perhaps I overseen something....
Re: Getting root object from c++
Accessing QML properties from C++ is the same no matter where you are in your C++ code, main or not.
Notice that the heading of that segment reads "Accessing Members of a QML Object Type from C++" and not "Accessing Members of a QML Object Type from main.cpp".
In addition, in that section of the documentation main is not mentioned at all, the code examples there are totally agnostic to any scope in the code.
Re: Getting root object from c++
Hello,
ok I make it clearer.
Code:
Rectangle{
id::root
property string mystring:"Hello"
Button{
id: btnTest
width:140
height: 30
text: "btnTest"
onClicked: {
console.log(root.mystring)
}
MyPlugin{
id:plugin1
Component.onCompleted: {
}
}
}
When I load my qml file through qml.exe it loads the qml file and my property string mystring is equal to "Hello".
Then in MyPlugin::componentComplete I make this:
Code:
QQmlEngine *mrtEngine = qmlEngine(this);
QQmlComponent component(mrtEngine, "path/to/file.qml");
QObject *object
= component.
create();
qCritical() << "Property value:" << QQmlProperty::read(object, "mystring").toString();
QQmlProperty::write(object, "mystring", "Hello QtCentre");
delete object;
So now when I click my Button mystring is equal to "Hello" and not "Hello QtCentre"?!?
Re: Getting root object from c++
Which qml file are you loading with 'path/to/file.qml'?
The QML file posted above with the root object?
And you are doing it from the plugin code?
If so this makes no sense to me, as your plugin creates a new root object where itself is also initialized??
otherwise please elaborate more.
Re: Getting root object from c++
this is my "path/to/file.qml"
Code:
Rectangle{
id::root
property string mystring:"Hello"
Button{
id: btnTest
width:140
height: 30
text: "btnTest"
onClicked: {
console.log(root.mystring)
}
MyPlugin{
id:plugin1
Component.onCompleted: {
}
}
}
Yes you are right. The qml.exe is loading 'path/to/file.qml' and then my plugin is also handling the 'path/to/file.qml' file.
My goal is to modify loaded qml file through the plugins in it...
Re: Getting root object from c++
The C++ code you posted, is it in your plugin or application?
Re: Getting root object from c++
It is in my Plugin as I said
Quote:
Then in MyPlugin::componentComplete I make this:
Code:
QQmlEngine *mrtEngine = qmlEngine(this);
QQmlComponent component(mrtEngine, "path/to/file.qml");
QObject *object
= component.
create();
qCritical() << "Property value:" << QQmlProperty::read(object, "mystring").toString();
QQmlProperty::write(object, "mystring", "Hello QtCentre");
delete object;
Re: Getting root object from c++
At least for me it hard to follow.
It seems something in your setup, is wrong, but its hard to say what without seeing the full picture.
Try uploading your project as a zip here and we can then take a better look.