[Qt-interest] Using qSort with lists of QPointers

Rohan McGovern rohan.mcgovern at nokia.com
Mon Sep 6 00:50:10 CEST 2010


Frederico A. M. Prado said:
> Yes, it is smart enough to call the operator in the class with the correct  
> arguments.
> 
> Lets say your class is this:
> 
> class Interval {
> public:
>     bool operator <(const Interval& other) const {return  
> (intervalBegin<other.intervalBegin);}    
> private:
>     int intervalBegin;
>     int intervalEnd
> }
> 
> if you call qSort on a list of this class it will compare each object  
> using the above function and sort those objects by their members  
> "intervalBegin" and not by their pointer addresses or any other member.

The OP asked about a list of QPointers.

If, in your example, you had QList<QPointer<Interval> > mylist, (setting
aside that it wouldn't compile since Interval isn't a QObject),
qSort(mylist.begin(), mylist.end()) would _not_ call your operator< ,
it would sort by pointer value as the OP suspected.
-- 
Rohan McGovern
QA Engineer
Qt Development Frameworks, Nokia



More information about the Qt-interest-old mailing list