PDA

View Full Version : Getting root object from c++



codeman
26th June 2017, 11:05
Hello freinds,

I have a question. Say I have this



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

codeman
10th July 2017, 17:24
Seems to be a very stupid question from me?!?!?!

high_flyer
13th July 2017, 16:33
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-cppintegration-interactqmlfromcpp.html
(see Accessing Members of a QML Object Type from C++)

codeman
13th July 2017, 17:58
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....

high_flyer
14th July 2017, 12:31
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.

codeman
14th July 2017, 14:02
Hello,

ok I make it clearer.


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:


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"?!?

high_flyer
14th July 2017, 15:01
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.

codeman
14th July 2017, 15:12
this is my "path/to/file.qml"


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

high_flyer
15th July 2017, 23:33
The C++ code you posted, is it in your plugin or application?

codeman
17th July 2017, 10:57
It is in my Plugin as I said

Then in MyPlugin::componentComplete I make this:


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;

high_flyer
17th July 2017, 13:46
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.