Interacting with QML Objects from C++
-
@Khachatur Just tested your code and found out that if you remove this
if
conditionif(aRoot != NULL) { ...
it works. Can you confirm ?
However I'm too not sure why this works if the condition is removed. -
I have removed that condition and it is still not working...
Is it possible that rootObjects or findchild return pointer to the copy of original object?
-
@Khachatur No I guess. Because changing property of those objects reflects the changes.
Try removing the secondif
condition too. -
I am new in qt and qml, therefore sorry... It was my bad... again :(
The code below is correct when you are loading the main.qml first time in main.cpp and then get pointer to root object.
QQmlApplicationEngine anEngine; anEngine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *aRoot = anEngine.rootObjects()[0]; QQuickWindow* aWindow = qobject_cast<QQuickWindow* >(aRoot); if (aWindow) { aWindow->show(); }
I have used code above in my operation class to get pointer to root object of main.qml.
But such approach is incorrect, because it will not return the pointer to the root object of main.qml, but will create new instance of main.qml and will return pointer to the root object of new created.
To get the root object of already displayed main.qml file I am using:
QQmlEngine *anEngine = QQmlEngine::contextForObject(myViewer)->engine(); QObject* aRoot = ((QQmlApplicationEngine*)anEngine)->rootObjects()[0];
Where myViewer has C++ class type and is registered as qml type
qmlRegisterType<Viewer>("Viewer", 1, 0, "Viewer");
and defined in main.qml.Currently, everything is working, I can change properties of qml objects and can recieve signals from qml.
But, is there another way to get poiner to the root object of main.qml?
P.S. Thank you for your response
-
But such approach is incorrect, because it will not return the pointer to the root object of main.qml, but will create new instance of main.qml and will return pointer to the root object of new created.
Do you mean there are two instances of main.qml ? Which are they ?
-
Yes, I mean that there are two instances of main.qml.
Doing this you will get the new instance of main.qml:
QQmlApplicationEngine anEngine; anEngine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *aRoot = anEngine.rootObjects()[0];
I didn't get what does it mean "Which are they"?
-
@Khachatur How did you check whether they are 2 separate instance ?
-
I don't know the way how to check directly whether they are 2 separate instances, but I was relying on logic.
In QML slots onColorChanged and onVisibleChanged of test_rect I am printing the 'color' and 'visible' values.
onColorChanged: { console.log(color) } onVisibleChanged: { console.log(visible) }
When I am changing property of QML objects from C++ code:
aRect->setProperty("color", "green"); aRect->setVisible(false);
In output console I get the correct values of color and visibility (green and false respectively), but rectangle still remains red and visible.
Therefore I make decision that I am working with another instance of main.qml and I was right.
-
@Khachatur No they are exactly the same objects.
Consider the following example:
main.qmlimport QtQuick 2.4 import QtQuick.Window 2.2 Window { visible: true width: 150 height: 150 Rectangle { anchors.fill: parent } Component.onCompleted: console.log("From QML: ",this) // prints the root object }
your main.cpp
QQmlApplicationEngine anEngine; anEngine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *aRoot = anEngine.rootObjects()[0]; qDebug() << "From C++: " << aRoot;
After running if you look at the outputs you can see the same address on both sides which indicates it is the same object.
I think there is some other problem in your code.
Can you post or upload an updated complete minimal working example which will show the problem ? -
I have checked.
This code I am using at the first time in file main.cpp when I am launching the application
QQmlApplicationEngine anEngine; anEngine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *aRoot = anEngine.rootObjects()[0]; qDebug() << "From C++: " << aRoot;
Then I am using the same code in my OperationCreatePoint.cpp class and I get the different address of aRoot.
The output is:
From main.cpp: ApplicationWindow_QMLTYPE_57_QML_74(0x4711600)
From OperationCreatePoint.cpp: ApplicationWindow_QMLTYPE_143_QML_160(0x8478fc0) -
@Khachatur Some confusion. Do you mean you load
main.qml
twice ? One ismain.cpp
and another inOperationCreatePoint.cpp
?
Atleast from the output it seems. -
English is not my native language, may be you misunderstood me... Anyway thank you for your response and sorry for misunderstood
-
@Khachatur That's fine :)
If you still have the problem you can post the updated complete minimal working example so that it would be more helpful in finding the problem. -
Yes, I meant I was loading
main.qml
twice inmain.cpp
andOperationCreatePoint.cpp
. Currently, I have not any problem, Thanks :)I just have another question about QML
FileDialog
component, but I guess... should I create new thread?I am using the
FileDialog
to load and save files. When I am saving file I want to pass to myFileDialog
the default file name, but unfortunatelyFileDialog
have not such property, onlyfileUrl
property which is ReadOnly. I don't want to implement my own file dialog component... -
@Khachatur AFAIK it is still not implemented. Taking this into consideration some one has already implemented their own. See here.
-
That is exactly what I need. Thank you :)