Luca
Luca

Reputation: 10996

calling javascript in another file in qml

I am trying to override the onClosing event in a QML application window.

The qml for the window is simple as:

ApplicationWindow {
    id: rootWindow
    objectName: "window"
    visible: true
    width: 800
    height: 480

    property Component loginForm: LoginView {}

    onClosing: {
        loginForm.logout()
    }
}

The LoginView view is simple as well:

Rectangle {
    id: view    
    function logout() {
        console.log("Logout called");
    }
}

Now, as is, it returns an error:

TypeError: Property 'logout' of object QQmlComponent(0x9287150) is not a function

I also tried loginForm.view.logout() and this returns in:

TypeError: Cannot call method 'logout' of undefined

Upvotes: 1

Views: 493

Answers (1)

Ross Rogers
Ross Rogers

Reputation: 24218

I believe QML is having trouble because your property is of type Component. You are assigning a LoginView, which is an inheritance descendent of Component, to a property of type Component. If you change your property to be of type LoginView, it will work:

property LoginView loginForm : LoginView{}

If this isn't actually a property that you want to be exported by the main module, you can simply instantiate it without creating a property, but still giving it a module-scope identifier:

LoginView{ id: loginForm }

Doing either of these will give you access to that function.

Upvotes: 2

Related Questions