Results 1 to 4 of 4

Thread: Proper QList usage

  1. #1
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Proper QList usage

    Hi

    I am using Qlist of objects of type MyObj in an instance of class AClass

    Should I store the as a list of pointers to MyObj

    Qt Code:
    1. QList <MyObj*> mylist;
    To copy to clipboard, switch view to plain text mode 

    The function to add items to my list is as follows

    Qt Code:
    1. void AClass::addToList(const QList <MyObj> &items)
    2. {
    3. foreach(MyObj m, items)
    4. {
    5. MyObj mm = new MyObj(m);
    6. mylist.append(mm);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    OR a list of MyObj instances and i add to the list as follows

    Qt Code:
    1. QList <MyObj> mylist;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void AClass::addToList(const QList <MyObj> &items)
    2. {
    3. mylist.append(items);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Which of the two cases above is the best way?

    I have a problem removing items from the list as the MyObj class does not have a comparison operator

    For example, I have a function to remove items from the list

    Qt Code:
    1. void AClass::removeFromList(const QList <MyObj> &items)
    2. {
    3. foreach(MyObj m, items)
    4. {
    5. /*
    6.   what do I put in here if I want to remove 'items' from mylist
    7.   */
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Regards

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Proper QList usage

    This page will give you an overview of QList.

    Now my little advice: use QList (or other containers) of pointers mainly in two cases:
    1) if you have some classes that can't be copied (eg QObject derived classes)
    or,
    2) if you have "big" objects that are "hard" to copy.

    And when using container<sometype*> you can think of using container<smart_pointer<sometype> > instead, Qt has some smart_pointers, like QSharedPointer.

  3. #3
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Proper QList usage

    Thanks for the advice Zlatomir.

    MyObj is not so big so I have not use a QList of pointers, however how can I search the list (and replace/remove items) if MyObj does not have a comparison operator defined ?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Proper QList usage

    You can use list.removeAt(position);
    But, if you need to test for equality (like to find the position ) or compare objects of your type you need to overload the operators for your type.
    Or use some algorithms like std::find_if (that takes predicate that does the comparison) or code your own algorithms that take a predicate.

    One more thing, as i see you talk about removal from QList - i have to say QList is more similar to std::vector than std::list (read the link in my previous post) - in short: QList is not a linked-list, Qt has QLinkedList class that implements a linked-list data-structure.

  5. The following user says thank you to Zlatomir for this useful post:

    smick (28th September 2011)

Similar Threads

  1. QSqlQuery prepared statements proper usage
    By psih128 in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2011, 23:10
  2. proper use of QList?
    By jhowland in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2010, 14:57
  3. Proper QList usage
    By space_otter in forum Newbie
    Replies: 5
    Last Post: 22nd June 2010, 06:57
  4. Replies: 1
    Last Post: 13th November 2009, 01:45
  5. QList usage in place QPtrList
    By darpan in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 15:41

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.