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. [SOLVED] Q_INVOKABLE vs Q_PROPERTY (QML hanging...)
QtWS25 Last Chance

[SOLVED] Q_INVOKABLE vs Q_PROPERTY (QML hanging...)

Scheduled Pinned Locked Moved QML and Qt Quick
qinvokableqpropertyblocktypeisvali
3 Posts 2 Posters 14.3k Views
  • 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.
  • E Offline
    E Offline
    Erakis
    wrote on 16 Sept 2015, 01:27 last edited by Erakis
    #1

    Hi,

    I don't understand why using Q_INVOKABLE instead of Q_PROPERTY to return a QObject derivated class pointer method made my application crashing/hanging. Moreover I see this error message in my console

    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error
    

    Like the memory is double free somewhere....

    Here is how to reproduce :

    class ChildrenClass : public QObject
    {
        Q_OBJECT
    public:
        explicit SubClass(QObject *parent = 0);
        Q_INVOKABLE QString getData() {return "Hello world !";};
    }
    
    class ParentClass : public QObject
    {
        Q_OBJECT
    public:
        explicit ParentClass (QObject *parent = 0);
    
        QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
        {
             return new ParentClass ();        
        }
    
        Q_INVOKABLE ChildrenClass * childrenClass() {
             return &GlobalChildrenClass; // Children class is 
        };
    }
    

    In the main.cpp, I registered the types

    ChildrenClass   GlobalChildrenClass; // Static instance of a children class
    
    int main(int argc, char *argv[])
    {
          // Register ChildrenClass
        qmlRegisterType<ChildrenClass >("ChildrenClass", 1, 0, "ChildrenClass");
    
        // Create singleton for ParentClass
        qmlRegisterSingletonType<ParentClass>("ParentClass ", 1, 0, "ParentClass ", &ParentClass ::qmlInstance);
        ...
    }
    

    Now in the main.qml

    import ChildrenClass 1.0
    
    Window {
        id : mainWindow
        width :800
        height :480
        visible: true
        ...
     
        Component.onCompleted : {
            var g = ParentClass.childrenClass().getData(); // This is hanging the application
        }
    

    But If I do :

    class ParentClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(ChildrenClass * childrenClass READ childrenClass)
    public:
        explicit ParentClass (QObject *parent = 0);
    }
    
    ...
        Component.onCompleted : {
            var g = ParentClass.childrenClass.getData(); // This is well working :) !
        }
    

    It is a bug or QML JS engine is not able to retrieved a subclass pointer and then calling a function of this pointer ? Or maybe there something wrong with my implementation ?

    Best regards,

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Erakis
      wrote on 16 Sept 2015, 11:51 last edited by Erakis
      #2

      I finally understand by myself why it why not working :)

      The parent class is exporting a pointer to the QML side. As it is a pointer, the QML Engine take the ownership and then once he finished using it, he free memory.

      So I have to add this to the ParentClass

      explicit ParentClass (QObject *parent = 0)
      {
            QQmlEngine::setObjectOwnership(&GlobalChildrenClass, QQmlEngine::CppOwnership);
      }
      

      So I deduce that a property do not free memory of a pointer, while a Q_INVOKABLE yes !.

      Except also that a Q_PROPERTY can be notifiable, I don't know Finally I don't know any other difference between Q_PROPERTY vs _Q_INVOKABLE.

      Hope it will help somebody else :)

      Best regards,

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 17 Sept 2015, 19:09 last edited by
        #3

        Hi,

        Q_INVOKABLE is used to make functions accessible throughout Qt meta object system. Q_PROPERTY is much more complex and offers additional features

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        1/3

        16 Sept 2015, 01:27

        • Login

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