Results 1 to 4 of 4

Thread: Is there a Pointer Based QMap or Similar

  1. #1
    Join Date
    Apr 2006
    Posts
    1
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Unhappy Is there a Pointer Based QMap or Similar

    Hi,

    I have two classes a Model and a Dataset
    Suppose I need to create a map between two Pointers. for Egs
    Qt Code:
    1. QMap<Model*, DataSet* > mapModelPtrToDataSet;
    To copy to clipboard, switch view to plain text mode 

    The code is working and I get the DataSet* for the Model* That i give to the Map

    But Since I understood recently that the QMap is ValueBased... And all the DataSet* would not get delete in the Destructor of the Map. Is there an Pointer Based alternative ?

    Thanks in Advance

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

    Default Re: Is there a Pointer Based QMap or Similar

    No, but you should be able to use qDeleteAll(mapModelPtrToDataSet.values()) to do that.

  3. The following user says thank you to wysota for this useful post:

    vasudhamirji (4th April 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there a Pointer Based QMap or Similar

    But what if the DataSet* in the Map Gets Deleted Externally ?
    will the qDeleteAll cause seg faults ??
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Is there a Pointer Based QMap or Similar

    Yes, of course. But it's the programmers responsibility not to delete objects he/she doesn't own. If you want a QMap with pointers to objects you don't own and those objects inherit QObject, you can use:

    Qt Code:
    1. QMap<Model *, QPointer<DataSet> > map;
    To copy to clipboard, switch view to plain text mode 

    In Qt3 you can use QGuardedPtr instead of QPointer. With such a mechanism qDeleteAll() should not make any harm as QPointer sets its internal pointer to 0 when the object it references gets deleted. And deleting null pointers is safe. On the other hand deleting pointers which you don't own doesn't make much sense, so the situation is purely academic.

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.