Solved One single instance of a qml component used in all other qml files
-
Hello!
How is it possbile to create one single instance of a qml component (for example MyGlobalMessageBox.qml) and use this one instance in all other qml files?
Kind regards,
MHermann -
-
@MHermann
Create a instance with id in main.qml and use that id in other files.
eg.in main.qml..
MyGlobalMessageBox{ id: mGlobalMB ...... }
In other file
mGlobalMB.property = xyz or mGlobalMB.functionCall()
-
@sierdzio : Thanks for your answer.
Now I have done the following:
- qmldir:
singleton Msgbox 1.0 qrc:/QML/Vetron_MessageBox.qml
- MyGlobalMessageBox.qml:
pragma Singleton import QtQuick 2.0 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 import "." Rectangle { ... }
Now a get the following runtime errors:
QQmlApplicationEngine failed to load component qrc:/QML/DataHandler.qml:-1 Type Msgbox unavailable qrc:/QML/MyGlobalMessageBox.qml:-1 Msgbox is instantiated recursively
At the moment I am not using Msgbox in the programm.
What could be the reason for these errors? -
I think you don't need the qmldir, just the pragma declaration, but maybe I am mistaken. Also I don't think qmldir supports QRC paths, it should be a plain path instead.
-
@Tirupathi-Korla : Thanks for your answer.
From my "main.qml" I am creating the sub-components dynamically.
Therefore I can not access the ids of the "main.qml" or I don't know how to do that ... -
@sierdzio : Ok. And how can I access the MessageBox? Only via it's name?
MyGlobalMessageBox.x = ... ...
And if am using it this way in different qml files I will always use the same instance of the MessageBox?
-
Yes, it should be accessible via file name and point always to the same instance.
-
@MHermann
As you want it as a single instance, you may create in main.qml without creating it dynamically asMyGlobalMessageBox{ id: mGlobalMB ...... }
with which you can access it in other qml files using its id.
-
@sierdzio : Ok. At the moment I am getting no errors. The code in MyGlobalMessageBox.qml is called. But the messagebox is not shown... Maybe it is because the parent of the messagebox is null. How can I resolve this problem?
-
@Tirupathi-Korla : Ok. I did it like you recommended.
Main.qml:
Page { id: mainId MyGlobalMessageBox { id: myGlobMsgBox } Rectangle { ... GridLayout { ...
But I get "ReferenceError: myGlobMsgBox is not defined" when I try to access myGlobMsgBox in an other qml file. As I said before maybe it is because all other components are created dynamically in Main.qml. And it these other qml files I can not acces myGlobMsgBox neither mainId.
-
@MHermann
May i Know whats Page?? and is Page inside any ApplicationWindow?? and what ever you create dynamically are inside Page.. Right!!!!And Vetron_MessageBox is a qml file.. right!!!
-
@Tirupathi-Korla :
Page is loaded by a Loader in SwipeView in my MainPageManagement.qml. And this SwipeView is contained in an ApplicationWindow.
Yes. All dynamically created qml components are inside page.
I corrected my post before. I meant MyGlobalMessageBox... -
@MHermann
Then better create Vetron_MessageBox inside ApplicationWindow.. so that it can be visible to all files.. -
@Tirupathi-Korla : Now I changed some things in my structure.
Now I can access myGlobMsgBox and the message box is working.