Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Q_INVOKABLE funciton returning null when called in QML 'onDestruction'
Forum Updated to NodeBB v4.3 + New Features

Q_INVOKABLE funciton returning null when called in QML 'onDestruction'

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Circuits
    wrote on last edited by Circuits
    #1

    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?

    jeremy_kJ 1 Reply Last reply
    0
    • C Circuits

      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?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @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.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Circuits
        wrote on last edited by Circuits
        #3

        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.

        jeremy_kJ 1 Reply Last reply
        0
        • C Circuits

          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.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @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.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Circuits
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved