Results 1 to 5 of 5

Thread: QList of pointers

  1. #1
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QList of pointers

    I have such a code:
    Qt Code:
    1. static QList<display_list*> displayLists;
    To copy to clipboard, switch view to plain text mode 
    items are added this way:
    Qt Code:
    1. list = new display_list();
    2. list->new_list(GL_COMPILE);
    3. glBegin(GL_QUADS);
    4. ...
    5. glEnd();
    6. list->end_list();
    7. displayLists.append(list);
    To copy to clipboard, switch view to plain text mode 
    The problem is if I need to clear list and generate otehr display lists than displayLists.clear() is a bad choice - ~display_list() is not called for elements, in fact lists still reside in OpenGL memory. Currently I have such a header in function, that generates this list:
    Qt Code:
    1. void prepareLists()
    2. {
    3. if(!displayLists.isEmpty())
    4. foreach(display_list* ptr, displayLists) delete ptr;
    5. displayLists.clear();
    6. ...
    To copy to clipboard, switch view to plain text mode 
    but I don't like it
    Any ideas how to make it nice&clean?

    PS: display_list is not a QObject

  2. #2
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QList of pointers

    hmm... will
    Qt Code:
    1. class MyQList: public QList
    2. {
    3. void QCollection::deleteItem ( Item d ) {
    4. delete d;
    5. ::deleteItem(d);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    do the job?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList of pointers

    Do you know boost? You could use boost::shared_ptr. Please don't try this with std::auto_ptr because std:auto_ptr becomes invalid after reconstruction in / by QList.

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

    Default Re: QList of pointers

    Qt Code:
    1. void prepareLists()
    2. {
    3. qDeleteAll(displayLists);
    4. displayLists.clear();
    5. //...
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QList of pointers

    I've moved to boost::shared_ptr, Codepoet thanks for the idea (I'm boost user for several weeks, don't remember all its features )

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 00:06
  3. Destroying a QList the right way
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 19th January 2009, 10:52
  4. QList Pointers
    By coderbob in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 18:50
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

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.