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. QtQuick Control Q_INVOKABLE returning null
Forum Updated to NodeBB v4.3 + New Features

QtQuick Control Q_INVOKABLE returning null

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 2.1k 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.
  • B Offline
    B Offline
    bratao
    wrote on last edited by
    #1

    Guys, I have a big problem in QtQuick/QML for a while and I´m helpless = (
    I´m using Qt 5.2 nightly. I have a QAbstractListModel that is connected to QtQuick Control TreeView. If I click on it in the signal ¨onCurrentRowChanged¨ I call a function of this QAbstractListModel called “Get” to get a QObject*. This object is used to show details in the interface.
    It do work great for a while, but suddenly when alternating the rows, it will stop working. QML complains that the returned object is NULL.
    But Debuging the c++ “get” function, I can see that it is returning a valid object. Please, someone know what is happening ???

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      QML ownership semantics of Q_INVOKABLE functions is that the QML engine will take ownership of the returned pointer, and delete the object during garbage collection if no javascript references to the object exist.

      To keep it alive, either manually store a reference to the returned object, or modify the ownership of the object to CppOwnership manually.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bratao
        wrote on last edited by
        #3

        Chris,
        You solve it !!

        Thanks a lot !!!

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jrlaff
          wrote on last edited by
          #4

          Replying to share my personal experience with this issue.

          Changing the ownership of the instantiated object to "QQmlEngine::CppOwnership":http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum before returning it indeed fixes the issue.

          What is not very intuitive though is the fact that you have to change the ownership to CppOwnership even if the objectOwnership() method already returns CppOwnership. This gives something like:

          @QObject *
          MyClass::InvokableMethod ( ... )
          {
          QObject * pObject = InstanciateObject( ... );

          QQmlEngine::ObjectOwnership ow = engine->objectOwnership( pObject );
          // Here, ow == QQmlEngine::CppOwnership

          engine->setObjectOwnership( pObject, QQmlEngine::CppOwnership );

          return pObject;
          }@

          The "setObjectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership and "objectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership methods seem to perform more stuff than just set and get a two-value property on the object, something that does not appear in the documentation.

          Anyway, thanks for the post.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jrlaff
            wrote on last edited by
            #5

            Replying to share my personal experience with this issue.

            Changing the ownership of the instantiated object to "QQmlEngine::CppOwnership":http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum before returning it indeed fixes the issue.

            What is not very intuitive though is the fact that you have to change the ownership to CppOwnership even if the objectOwnership() method already returns CppOwnership. This gives something like:

            @QObject *
            MyClass::InvokableMethod ( ... )
            {
            QObject * pObject = InstanciateObject( ... );

            QQmlEngine::ObjectOwnership ow = engine->objectOwnership( pObject );
            // Here, ow == QQmlEngine::CppOwnership

            engine->setObjectOwnership( pObject, QQmlEngine::CppOwnership );

            return pObject;
            }@

            The "setObjectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership and "objectOwnership()":http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership methods seem to perform more stuff than just set and get a two-value property on the object, something that does not appear in the documentation.

            Anyway, thanks for the post.

            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