Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Q_INVOKABLE funciton returning null when called in QML 'onDestruction'



  • I have a couple of void functions which are declared as Q_INVOKABLE in the business logic:

    Q_INVOKABLE void foo();
    

    like so and these functions are being called in the QML like so:

            Component.onDestruction:
            {
              foo()
             ...
    

    this works the way I want it to when the QML pages are being popped off the stack; however, they are also called when the program closes and that is throwing errors:

    TypeError: Cannot call method 'foo' of null
    

    I assumed this is happening because the method in the business logic is being destroyed prior to the call in the QML but when I tried running a check against undefined:

    if(foo() !== undefined)
    ...
    

    or

    if(typeof foo() === "function")
    

    or

    if(typeof (foo()) !== "undefined")
    

    it simply gave me the error on the if statement itself instead... so I am a bit confused as to what I am doing wrong here. I tried calling a Q_PROPERTY instead and got the same result. I tried calling a function that returns a different type as well and still got the same result.

    I am aware the the onDestruction ordering is undefined and I don't mind that but I would like to stop my program from segfaulting every time it's closed while on this QML page.

    EDIT: Perhaps there is another way of performing an action when the item is popped off the stack other than the onDestruciton signal?



  • @Circuits said in Q_INVOKABLE funciton returning null when called in QML 'onDestruction':

    TypeError: Cannot call method 'foo' of null
    

    I assumed this is happening because the method in the business logic is being destroyed prior to the call in the QML but when I tried running a check against undefined:

    if(foo() !== undefined)
    if(typeof foo() === "function")
    if(typeof (foo()) !== "undefined")
    

    Think of the call as obj.foo(). The error says that obj is null.
    All three of these tests are calling foo() and examining the return type. I think that the intention is if (foo !== undefined), if (typeof foo == "function"), and if (typeof foo !== "undefined"). None of these or the versions that actually attempt to call foo() will work because there is no object to look up the function in.

    I am aware the the onDestruction ordering is undefined and I don't mind that but I would like to stop my program from segfaulting every time it's closed while on this QML page.

    A minimal working example will probably make this easier to solve.



  • @Circuits said in Q_INVOKABLE funciton returning null when called in QML 'onDestruction':

    TypeError: Cannot call method 'foo' of null
    

    I assumed this is happening because the method in the business logic is being destroyed prior to the call in the QML but when I tried running a check against undefined:

    if(foo() !== undefined)
    if(typeof foo() === "function")
    if(typeof (foo()) !== "undefined")
    

    Think of the call as obj.foo(). The error says that obj is null.
    All three of these tests are calling foo() and examining the return type. I think that the intention is if (foo !== undefined), if (typeof foo == "function"), and if (typeof foo !== "undefined"). None of these or the versions that actually attempt to call foo() will work because there is no object to look up the function in.

    I am aware the the onDestruction ordering is undefined and I don't mind that but I would like to stop my program from segfaulting every time it's closed while on this QML page.

    A minimal working example will probably make this easier to solve.



  • Is there a way to test if the object itself is null from the QML?

    I have a workaround. I created two states: open, closed and a property:

    property string prevState: ""
    

    whenever the state changes I set the previous state to be the current state that way I can identify when the state is changing from open to closed and in that instance I can call the methods.

    However, while this works it doesn't explain how to deal with the QMl calling an object which is null.



  • @Circuits said in Q_INVOKABLE funciton returning null when called in QML 'onDestruction':

    Is there a way to test if the object itself is null from the QML?

    The test is the same as for any variable. if (! obj) tests for null or undefined. Using an explicit !== null also works. Presumably the object in this case is this, but getting into the situation where this === null is something you probably want to avoid.

    I have a workaround. I created two states: open, closed and a property:

    property string prevState: ""
    

    whenever the state changes I set the previous state to be the current state that way I can identify when the state is changing from open to closed and in that instance I can call the methods.

    However, while this works it doesn't explain how to deal with the QMl calling an object which is null.

    Without code, I can't suggest anything better than if (this) { do_something(); }, which looks like a work around for a deeper problem.



  • This post is deleted!

Log in to reply