Interacting with QML Objects from C++
-
@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 :)