Results 1 to 6 of 6

Thread: Store QWeakPointers in QSet

  1. #1
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Store QWeakPointers in QSet

    Hi,

    I'm trying to store QWeakPointers in a QSet with no success. The compiler first complained about QWeakPointer is lacking a qHash function. I Implemented a qHashFunction, the project compiled but I can't fetch data from the QSet (tried iterators). It only returns invalid QWeakPointers.

    Basically I'm trying to do:
    Qt Code:
    1. QSet<QWeakPointer<MyClass> > oneSet;
    2. QSharedPointer<MyClass> > myClass(new MyClass);
    3. oneSet.insert(oneSet.toWeakRef());
    4.  
    5. for (QSet<QWeakPointer<MyClass> >::iterator it = oneSet.begin(); it != oneSet.end(); it++)
    6. {
    7. // do stuff
    8. }
    9.  
    10.  
    11. //My qHash:
    12. template<typename T>
    13. uint qHash(const QWP<T> &p)
    14. {
    15. return qHash(p.data()); //use the pointer a a key.
    16. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone have a solution for this?
    Last edited by high_flyer; 22nd August 2013 at 16:49. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Store QWeakPointers in QSet

    Can you explain what the following line is supposed to do?
    Qt Code:
    1. oneSet.insert(oneSet.toWeakRef());
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Store QWeakPointers in QSet

    That is a typo. It should be:
    Qt Code:
    1. oneSet.insert(myClass.toWeakRef());
    To copy to clipboard, switch view to plain text mode 

    I chose to use QLinkedList instead of QSet. But I think I found the problem was. I believe that the qHash function was working, but I forgot to keep a QSharedPointer to the created objects. When the QSharedPointer went out of scope all the newly created objects were deleted, and only left the weak pointers behind. When I tried to iterate the list it couldn't promote the weak pointer to a strong pointer.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Store QWeakPointers in QSet

    Why not have a QList<QSharedPointer<MyClass> >?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2013
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Store QWeakPointers in QSet

    I need to have QWeakPointer because of cyclic reference. MyClass holds a QSharedPointer to a child MyClass and a QWeakPointer to a parent MyClass.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Store QWeakPointers in QSet

    And where is this cyclic reference?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. modifying QSet via STL iterator
    By tuli in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2013, 08:34
  2. QSet of QDateTime
    By Cucumber in forum Newbie
    Replies: 2
    Last Post: 21st January 2012, 02:17
  3. Using custom class as a key (QSet)
    By daujeroti in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2011, 20:39
  4. QSet<QVariant> is supported ?
    By nikhilqt in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2010, 07:14
  5. QSet and custom class
    By been_1990 in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2010, 16:36

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
  •  
Qt is a trademark of The Qt Company.