Important: Please read the Qt Code of Conduct -

Newbie error

  • Hi there

    I am brand new to QT creator and I am having a problem I was hoping that someone could help with. I have a project with a few screens - .qml file for each of the screens. I have a button on one screen and when the user presses the button I want to navigate/load another screen.

    I have the following code -

    @ ImageButton {
    id: image_button1
    x: 70
    y: 214
    width: 132
    height: 80
    text: "OUTPUTS"
    font.bold: false "Arial"
    imageUp: "images/internal_button_up.bmp"
    font.pixelSize: 18
    textColor: "#000000"
    imageDown: "images/internal_button_dn.bmp"
    onButtonClick: root.message("../src/mainmenu.qml");

    The line - onButtonClick: root.message("../src/mainmenu.qml should take the user to another screen when the button is pressed. However when I run the code I get the following error - "ReferenceError: Can't find variable: root". The file mainmenu.qml is held in the project folder of the same project as the file that contains the above code.


  • Moderators

    IDs are not accessible from other QML files. If your "root" is defined in mainmenu.qml, then it is not visible in any other QML file.

  • Can you suggest a way to switch between the files? ie click on say an exit button and have it take you back to a main menu?

  • Moderators

    Use a Loader in MainMenu, and communicate using signals and slots mechanism. This is probably too involved for a newbie.

    Another option is the solution found in Qt Examples and other demos: store all pages in a single QML file, and switch between them using States and Transitions.

    All this is in the documentation, just give it a search :)

Log in to reply