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. Change ownership for value type property

Change ownership for value type property

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 520 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.
  • S Offline
    S Offline
    squareroot13
    wrote on last edited by squareroot13
    #1

    According to Qt documentation:
    """
    Data Ownership

    When data is transferred from C++ to QML, the ownership of the data always remains with C++. The exception to this rule is when a QObject is returned from an explicit C++ method call: in this case, the QML engine assumes ownership of the object, unless the ownership of the object has explicitly been set to remain with C++ by invoking QQmlEngine::setObjectOwnership() with QQmlEngine::CppOwnership specified.
    """
    It means that returned object from call of getter function of properties doesn't change a ownership.
    When i have some, derived from QObject class with properties of simple type(int, QString...), registered in QML (via qmlRegisterSingletonType) and used as CppOwnership instance.

    class TCustomDraw: public QObject
       {
           Q_OBJECT
       public:
           Q_PROPERTY (QColor color READ colorValue NOTIFY colorChanged)
           QColor colorValue() const
           {
               return m_color;
           }
       signals:
           void colorChanged();
       private:
           QColor m_color;
       };
    

    And QML main window:

    import QtQuick 2.12
        import QtQuick.Controls 2.12
        import QtQuick.Controls 1.4
        import QtQuick.Controls.Styles  1.4
        import QtQuick.Dialogs 1.1
        import QtQuick.Layouts 1.0
        ]import QtQuick.Window 2.12
        import Qt.labs.platform 1.1
        import QtQml 2.12
        import QtQuick.Shapes 1.12
        import customdraw 1.0
         
        Window {
            id: wnd
            width: 640
            height: 480
            visible: true
            Button {
                anchors.fill: parent
                onClicked: {
                     var f = TCustomDraw.color 
                }
            }
        }
    

    What happens with ownership of color object when i press the Button ?
    If ownership of color object remains in c++ does it mean a memory leakage ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's the QObject ownership that is concerned not the value type it handles.

      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
      0
      • S Offline
        S Offline
        squareroot13
        wrote on last edited by squareroot13
        #3

        @SGaist said in Change ownership for value type property:

        Hi,

        It's the QObject ownership that is concerned not the value type it handles.

        I think the same, but meaning of qt documentation at this place is ambiguous.

        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