1

I am trying to compare the items in a QList. Here is the old way to do it using QPtrCollection but this cannot be used in versions after qt3 (as far as I'm aware).

class gnyComponentList:public QList<gnyComponent> { protected: virtual int compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 ) { return (((gnyComponent *)item1)->getID()).compare(((gnyComponent *)item2)->getID());} }; 

I can't figure out what a good way of doing this in Qt5.3 might be?

2 Answers 2

2

You can use the std::equal algorithm on QList objects, as in:

#include <QList> #include <QString> #include <algorithm> // for std::equal struct Person { QString firstName; QString lastName; }; int main() { QList<Person> personsA, personsB; // Populate personsA and personsB bool equal = std::equal( personsA.begin(), personsA.end(), personsB.begin(), []( const Person &a, const Person & b ) { return a.firstName == b.firstName; } ); } 
Sign up to request clarification or add additional context in comments.

1 Comment

And what would I pass instead of QPtrCollection::Item item1, QPtrCollection::Item item2 to perform this?
0

This is a simple one, which compares every item without sorting. Here is the code.

bool TeachTab::isTwoStringListEqual(const QStringList &dst, const QStringList &src) { if (dst.size() != src.size()) return false; for (int i = 0; i < dst.size(); ++i) { if (dst.value(i) != src.value(i)) { return false; } } return true; } 

1 Comment

What happens when you compare two empty lists?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.