Important: Please read the Qt Code of Conduct -

How do I refer to the previous QML file after a new one is loaded?

  • I have a main.qml file, which loads (using Loader{}) another QML file mainWindow.qml (from a different file directory).

    Now, both these files are ApplicationWindow{} type, but I'd like to convert mainWindow.qml to Item{} type so that everything is loaded into the same application window.

    mainWindow.qml has a few properties like header, footer which are not Item{} properties. So I'd like to refer those properties to the main.qml file. How do I do that?

    I tried to simply write mainID.header and mainID.footer (where mainID is the id of main.qml file), but it says its a non-existent property.

  • Moderators

    Perhaps you should rather use Page component instead of Item.

  • @sierdzio While that does help a bit, it still leaves a few properties out such as onClosing: etc.
    Any idea how I can proceed?

  • Moderators

    Make sure your main.qml constructs an ApplicationWindow - it's id will be globally accessible.

    Regarding onClosing in particular - if your main window is in main.qml then you should not even try doing anything with it in a subcomponent ;-) If you need to get some information between those components - use signals and slots, or property assignments.

    I can help you with that but I need more info, like a minimal use-case.

  • So the thing is, I added the main.qml later. Hence the id of that ApplicationWindow isn't globally accessible, but the id of mainWindow.qml is.
    Meaning, initially the main QML file of the application was mainWindow.qml. Then I added another file main.qml and made that the initial QML file. So now I just want mainWindow.qml to just be loaded into the new ApplicationWindow as a Page or Item.
    I hope you understood what I meant.

  • Moderators

    QML code does not remember what it used to be. If you change main.qml to be your ApplicationWindow, it will be the application window, and it's id will be global.

    And even if the id was not global, it's easy to pass relevant information to other components via properties or signals & slots. Here's a simple example:

    // main.qml
    ApplicationWindow {
      id: mainWindow
      Component {
        id: myPage
        MyPage {
      StackView {
        id: stack
        initialItem: myPage
        onCurrentItemChanged: currentItem.text = "Some text!"
    // MyItem.qml
    Rectangle {
      property alias text: centralText.text
      Text {
        id: centralText
        text: "Default"

  • Unfortunately, that doesn't seem to work :/
    I tried to refer to the id of the new ApplicationWindow in MyItemq.qml but it just says that its a non-existent property

Log in to reply