Results 1 to 6 of 6

Thread: Proper QList usage

  1. #1
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Proper QList usage

    Should I use QList<MyClass> or QList<MyClass *>? And why doesn't QList support abstract classes? Isn't that an important feature?

    I am developing a subclass of QList<MyClass *> called "stack" which adds some extra operations on its objects and is also an item model so it can be viewed. These operations also save a copy of the list into a list of QList<MyClass *>. But when I try to use recalled list, the pointers are invalid!

    Some members of Stack:
    Qt Code:
    1. QList<QList<DElement*>*> history;
    2. void undo();
    3. QList<DElement> * duplicate();
    4. void nextHistoryStep();
    To copy to clipboard, switch view to plain text mode 

    nextHistoryStep is called when an operation is complete. The current state is appended to history. Or so I think undo is supposed recall the previous state by copying the pointers back.

    Here are the functions:
    Qt Code:
    1. void Stack::nextHistoryStep()
    2. {
    3. history.append(duplicate());
    4. }
    5.  
    6. QList<DElement> * Stack::duplicate()
    7. {
    8. QList<DElement*> * now = new QList<DElement*>();
    9. for(int i = 0; i < size(); i++)
    10. {
    11. now->append(at(i));
    12. }
    13. return now;
    14. }
    15.  
    16.  
    17. void Stack::undo()
    18. {
    19. if(history.size() > 1)
    20. {
    21. this->clear();
    22. // reload from last history
    23. QList<DElement*> * prev = history.at(history.size() - 2);
    24. for(int i = 0; i < prev->size(); i++)
    25. {
    26. append(prev->at(i));
    27. }
    28.  
    29. emitUpdate();
    30.  
    31. history.removeLast();
    32. delete prev;
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    Deleting a QList destroys that contents too. Is that just the internal pointers (in this case a pointer to pointer) or my actual objects? This code runs without a problem-but the instant that the contents are actually accessed, a segfault occurs. It is possible to delete the unneeded QList without destroying the contents and is this the problem?

    Oh, and DElement was originally abstract. It's a generic "element." But this caused a weird compiler error.

  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: Proper QList usage

    Are you sure the code you posted is correct? It shouldn't compile... QList<DElement> and QList<DElement*> types are not compatible and you are treating them as such, so there is obviously some typo here.
    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.


  3. #3
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Proper QList usage

    Ah, sorry, they should all be QList<DElement *> *.

    Will there be a memory leak if I remove "delete prev;"? The Stack destructor deletes the list contents, so I guess not. Removing the line didn't make it work. Seems the pointers are invalid before undo() is called.

    Whew! I figured it out. The operators were deleting the objects. Sorry, there was no way you could have known that. I'm developing an arbitrary-precision RPN calculator program. Do you think I should start a sourceforge project? It's still in the very early stages obviously...
    Last edited by space_otter; 18th June 2010 at 19:27.

  4. #4
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Proper QList usage

    Does QList support abstract classes though?

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Proper QList usage

    Quote Originally Posted by space_otter View Post
    Does QList support abstract classes though?
    What do you mean? You can store anything you like in a QList, but if you're trying to store classes derived from some abstract base class, you'll have to store pointers to the base class, not the derived classes themselves. That's how C++ handles such things.

    QList itself, however, doesn't care what you store in it, as long as all the elements match the template type.

  6. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Proper QList usage

    Quote Originally Posted by space_otter View Post
    Should I use QList<MyClass> or QList<MyClass *>? And why doesn't QList support abstract classes? Isn't that an important feature?
    QList doesn't support abstract classes, but pointers to abstract classes are no problem. You cannot instantiate an abstract class and QList has to be able to create the objects to work properly. If you are looking into storing abstract classes you will have to resort to pointers in a QList. I'm pretty sure the std containers don't handle them either.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  2. Replies: 1
    Last Post: 13th November 2009, 01:45
  3. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  4. QList usage in place QPtrList
    By darpan in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 15:41
  5. Proper way to #include Qt?
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2006, 02:15

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.