Results 1 to 5 of 5

Thread: Comparison of objects from QList

  1. #1

    Default Comparison of objects from QList

    Hi guys, I'm really new to Qt and I am wondering if there is a way to compare a passed item to the an entire QList.

    An example of what I am trying to do is as follows:

    Qt Code:
    1. class gnyComponentList:public QList<gnyComponent>
    2. {
    3. protected:
    4. virtual int compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 )
    5. { return (((gnyComponent *)item1)->getID()).compare(((gnyComponent *)item2)->getID());}
    6. };
    To copy to clipboard, switch view to plain text mode 


    This worked with older versions of Qt, but from what I have read so far I can't use QPtrCollection any more.
    Note: I am not looking to compare 2 lists to each other, just receive a value and compare it to the list to see if it is there.

    Thanks in advance for any help!

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Comparison of objects from QList

    receive a value and compare it to the list to see if it is there.
    maybe with std::find_if :
    Qt Code:
    1. //btw. I'd rather use private inheritance (if any), in order to prevent gnuComponentList* to QList<gnyComponent>* conversions
    2. // (QList destructor is not virtual)
    3. class gnyComponentList:private QList<gnyComponent>
    4. {
    5. protected:
    6. virtual bool containsItem( gnyComponent * item )
    7. {
    8. const auto it = std::find_if(begin(),
    9. end(),
    10. [&](const gnyComponent& cmp){
    11. return cmp.getID().compare(item->getID()) == 0; // i dont know when gnyComponents are equal exactly
    12. });
    13. return it!=end();
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    Alternatively if you can change gnyComponent class, you can implement "operator==" and use QList::contains:
    Qt Code:
    1. class gnyComponentList:private QList<gnyComponent>
    2. {
    3. protected:
    4. virtual bool containsItem( gnyComponent * item )
    5. {
    6. return contains(*item); // requres gnyComponent to have an implementation of "operator=="
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Comparison of objects from QList

    What do you need it for?

    The main use case for compareItems() in older Qt versions was specify a comparison criterial when sorting a list.

    In Qt4 and onwards one can specify "less than" function at qSort() and achieve the same thing.

    Cheers,
    _

  4. #4

    Default Re: Comparison of objects from QList

    Hi!

    Thanks for your replies!

    Essentially the list contains components that may be different types of component (e.g a component is either an atom, data or a statement). What I want to do is run through the list and check if there is already an instance of the component (which may also be an atom, data or statement) there. The idea is that I will have a complete list of components and there will be no duplication of any component on the list.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Comparison of objects from QList

    If your QList has no particular structure, then there is nothing clever to do: just loop over all the elements until you find the one you are looking for. You can code the loop by hand (that should take about 3 lines of code) or call std::find_if or std::find_if_not with a suitable predicate.

    Alternatively, if such lookups are frequent and costly, you should use a set structure (e.g. a hash set or a tree set).

Similar Threads

  1. Replies: 1
    Last Post: 6th February 2014, 08:28
  2. QList contains problem with custom objects
    By saravanadel in forum Newbie
    Replies: 11
    Last Post: 25th January 2012, 14:23
  3. QList/QStringList comparison question
    By di_zou in forum Newbie
    Replies: 5
    Last Post: 28th October 2009, 14:35
  4. Replies: 0
    Last Post: 25th June 2009, 08:17
  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.