1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ahhhhh....Thanks for that! Yes, of course, it loses the specific information due to the cast to the generic component type...
QML has got some funk to it. This tutorial was very helpful for me in the beginning: qmlbook.github.io
@Luca I think it would have worked with a generic type as well, i.e. Item or QtObject. Component is a "class" definition, not an instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.