How to create global id which is visible on the other qml files?
Solved
QML and Qt Quick
-
Thank you!
In main.qml file
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.Window 2.2 import QtQuick.Layouts 1.3 ApplicationWindow { visible: true width: Screen.width height: Screen.height title: qsTr("Hello World") property Loader parentLoader: pageLoader Loader { id: pageLoader; anchors.fill: parent; source: "Hello.qml" } }
and anywhere else:
Button { anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom anchors.bottomMargin: 30 width: 150 text: "CLICK ME" onClicked: pageLoader.setSource("MyQMLFile.qml", {"parentLoader": pageLoader}); }
-
Add a property "parentLoader" (for example) to first.qml, then when using Loader.setSource pass your loader (by it's id) to "parentLoader" via second parameter of setSource. In first.qml address the loader via parentLoader property instead of id.
pageLoader.setSource("first.qml", {"parentLoader": pageLoader});
-
Thank you!
In main.qml file
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.Window 2.2 import QtQuick.Layouts 1.3 ApplicationWindow { visible: true width: Screen.width height: Screen.height title: qsTr("Hello World") property Loader parentLoader: pageLoader Loader { id: pageLoader; anchors.fill: parent; source: "Hello.qml" } }
and anywhere else:
Button { anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom anchors.bottomMargin: 30 width: 150 text: "CLICK ME" onClicked: pageLoader.setSource("MyQMLFile.qml", {"parentLoader": pageLoader}); }