Results 1 to 3 of 3

Thread: Change ownership for value type property

  1. #1
    Join Date
    Jun 2024
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Change ownership for value type property

    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 call of getter function of properties doesn't change 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.
    Qt Code:
    1. class TCustomDraw: public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Q_PROPERTY (QColor color READ colorValue NOTIFY colorChanged)
    6. QColor colorValue() const
    7. {
    8. return m_color;
    9. }
    10. signals:
    11. void colorChanged();
    12. private:
    13. QColor m_color;
    14. };
    To copy to clipboard, switch view to plain text mode 
    And QML main window:
    Qt Code:
    1. import QtQuick 2.12
    2. import QtQuick.Controls 2.12
    3. import QtQuick.Controls 1.4
    4. import QtQuick.Controls.Styles 1.4
    5. import QtQuick.Dialogs 1.1
    6. import QtQuick.Layouts 1.0
    7. ]import QtQuick.Window 2.12
    8. import Qt.labs.platform 1.1
    9. import QtQml 2.12
    10. import QtQuick.Shapes 1.12
    11. import customdraw 1.0
    12.  
    13. Window {
    14. id: wnd
    15. width: 640
    16. height: 480
    17. visible: true
    18. Button {
    19. anchors.fill: parent
    20. onClicked: {
    21. var f = TCustomDraw.color
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    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 ?

  2. #2
    Join Date
    Oct 2024
    Posts
    1
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Change ownership for value type property

    Any update?
    Theresa Flanders

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,252
    Thanks
    304
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Change ownership for value type property

    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 ?
    You are confusing C++ instance lifetime with Qt QObject ownership lifetime. They aren't the same. C++ instances aren't owned by anything. They exist until whatever execution process they are created in goes out of scope, like in this case the function call to the color property. As soon as that function call exits, the QColor instance goes out of scope. The value held by that QColor instance is copied to the variable "f" in the QML code, and as soon as that statement ends the copy of m_color goes away, no memory leak.

    The QColor returned by the color property is returned by value, meaning it is placed on the stack as a copy of m_color. As soon as that instance goes out of scope, it is destroyed automatically. The QColor class is not derived from QObject. There is no concept of Qt ownership in the sense of other classes derived from QObject (such as QWidget, etc.).

    In this case, the QColor might as well be a fundamental type such as int or double. There is no magic going on, no transfer of ownership, no memory leak. The only way a memory leak could occur is if a method returned a pointer to an instance created by new() during the function call, and that pointer wasn't stored and later deleted on the receiving end.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Delegate y property change
    By clousque in forum Qt Quick
    Replies: 3
    Last Post: 6th September 2017, 17:19
  2. change each separator's property
    By cic in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2013, 10:24
  3. Change property of QInputDialog button
    By Raadush in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2012, 13:50
  4. Replies: 1
    Last Post: 20th February 2012, 21:44
  5. Change Stylesheet Using Dynamic Property
    By stefanadelbert in forum Qt Programming
    Replies: 4
    Last Post: 26th August 2010, 08:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.