Results 1 to 4 of 4

Thread: Enable shared from this QObject

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Enable shared from this QObject

    As far as I read in the documentation in order to be able to obtain QSharedPointer from within the object itself it is necessary to inherit from QEnableSharedFromThis. This makes me think of the case when the class must inherit from another class, e.g. QObject:

    Qt Code:
    1. class MyObject : public QObject, public QEnableSharedFromThis<MyObject>
    2. {
    3. public:
    4. void addChild(MyObject *child)
    5. {
    6. child->parent = sharedFromThis();
    7. }
    8.  
    9. private:
    10. QWeakPointer<MyObject> parent;
    11. };
    12.  
    13. QSharedPointer<MyObject> myObject(new MyObject);
    14. myObject->addChild(QSharedPointer<MyObject>::create().data());
    To copy to clipboard, switch view to plain text mode 

    Is it a correct approach? Multiple inheritance is often discouraged, but I don't see another way of doing this.
    Magicians do not exist

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Enable shared from this QObject

    QObject has that built in, see QObject::setParent(), QObject::parent(), QObject::children(), etc.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Enable shared from this QObject

    Well, QObject is for example here. It might have been another class.
    Also in case of QObject, it does not always have a parent, then we have a kind of mix of smart pointers with Qt parent/child model, which I'm not sure if it's good:
    Qt Code:
    1. QSharedPointer<MyObject> myObject(new MyObject); // smart pointer
    2. myObject->addChild(new MyObject); // naked pointer
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Enable shared from this QObject

    Quote Originally Posted by mentalmushroom View Post
    Well, QObject is for example here. It might have been another class.
    In the general case that looks like the way to go.

    Quote Originally Posted by mentalmushroom View Post
    Also in case of QObject, it does not always have a parent, then we have a kind of mix of smart pointers with Qt parent/child model, which I'm not sure if it's good:
    Indeed, only hold smart pointers to the top level node of a QObject tree.

    I was just pointing out that QObject is inherently capable of forming a parent/child tree with parent deleting it children when it is being deleted.

    Cheers,
    _

Similar Threads

  1. Enable QML debugging
    By poporacer in forum Newbie
    Replies: 0
    Last Post: 19th March 2012, 04:44
  2. QTableView enable
    By Daxos in forum Newbie
    Replies: 1
    Last Post: 30th June 2010, 08:47
  3. cant enable dbus
    By khcbabu in forum Installation and Deployment
    Replies: 1
    Last Post: 19th February 2009, 21:05
  4. enable layout
    By mickey in forum Newbie
    Replies: 6
    Last Post: 13th July 2006, 00:00
  5. How to enable QWS_IM in qte-3.3.5 ?
    By kevin lin in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th June 2006, 07:40

Tags for this Thread

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.