Results 1 to 3 of 3

Thread: Behaviour of hash when key is QPointer<QObject*>

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Behaviour of hash when key is QPointer<QObject*>

    I intend to use QPointer for pointer of my derived class from QObject as a key in QHash. According to QPointer documentation, the QPointer is made 0 after the object is deleted. If I use this QPointer in QHash, will the key be also made to 0? Or QHash makes the key by copying the int value of pointer, hence I will still be having the address of dangling pointer in my QHash?
    If somehow the key is made to zero, if multiple objects are deleted, will I end up with a Qhash of multiple entries for the same key?
    so somewhere in class.h
    Qt Code:
    1. QHash<QPointer<MyObject>, SomeObject*> hashSomeObject
    To copy to clipboard, switch view to plain text mode 
    somewhere in class.cpp as a global function
    Qt Code:
    1. uint qHash(const QPointer<MyObject>&ptr{
    2. return qHash(ptr.operator()->());
    3. }
    To copy to clipboard, switch view to plain text mode 
    somewhere in class.cpp
    Qt Code:
    1. hashSomeObject.insert(myObject1, new SomeObject());
    2. hashSomeObject.insert(myObject2, new SomeObject());
    3. hashSomeObject.insert(myObject3, new SomeObject());
    4. delete myObject1;
    5. delete myObject2;
    6. delete myObject3;
    To copy to clipboard, switch view to plain text mode 
    so what is hash now for the keys? Will the keys be made 0? Suppose I have a QList of QPointers, will they be carrying dangling addresses?

  2. #2

    Default Re: Behaviour of hash when key is QPointer<QObject*>

    Since your qHash Funktion returns an "uint" copy, i think the hash holds its old adresses. you can test it:

    Qt Code:
    1. QHashIterator<QPointer<MyObject>, SomeObject*> i(hash);
    2. while (i.hasNext()) {
    3. i.next();
    4. cout << i.key() << ": " << i.value() << endl;
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Behaviour of hash when key is QPointer<QObject*>

    I think it's a bad idea to have a QPointer in a hash as a key. Might be ok as a value, but definitely not as a key. The global qHash function caused immense pain in linking and it doesn't even have the qpointer facilities

Similar Threads

  1. Replies: 14
    Last Post: 4th February 2011, 20:51
  2. How to cast QPointer<T> to QPointer<childT>
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2010, 10:51
  3. Problem using QPointer
    By weaver4 in forum Newbie
    Replies: 8
    Last Post: 20th February 2010, 05:05
  4. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39
  5. QPointer and double deletion
    By mtrpoland in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:49

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