[Qt-interest] Using qSort with lists of QPointers

Frederico A. M. Prado famprado at terra.com.br
Mon Sep 6 00:07:13 CEST 2010


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.


On Sun, 05 Sep 2010 17:29:09 -0300, Ellen Kestrel <dragonflykes at gmail.com>  
wrote:

> Is qSort smart enough to actually call the overloaded operator on the
> object, though?  I mean, if you define, say, int *p and int *q and then  
> say
> p < q (rather than *p < *q), it will compare the values of the pointers
> (=addresses) and not the values of whatever they might be pointing to,  
> so it
> doesn't actually matter what they are pointing to (or if they are  
> pointing
> to anything at all).
>
> On Sun, Sep 5, 2010 at 3:10 PM, Frederico A. M. Prado  
> <famprado at terra.com.br
>> wrote:
>
>> Just implement the "<" operator in the class whose objects you want to
>> sort and in the implementation make sure you compare the value pointed  
>> by
>> your pointers and not the pointers themselves. You can do any comparison
>> you wish within the operator overload function.
>>
>> Something like:
>>
>> bool operator <(const MyClass& other) const {return
>> (this->comesBefore(other));}
>>
>>
>> On Sun, 05 Sep 2010 16:51:10 -0300, Ellen Kestrel  
>> <dragonflykes at gmail.com>
>> wrote:
>>
>> > I'm trying to use qSort to sort a list of QPointers to a class (which
>> > subclasses QObject).  There is a public lessThan function actually  
>> within
>> > the class itself, and when I was sorting the list by hand I simply
>> > called it
>> > from the class that manages the list.  Since I want to use qSort in  
>> the
>> > list-managing class, I defined a member function in that class with  
>> only
>> > this line of code:
>> >
>> > bool ListManager::objPrecedes (QPointer<MyClass> c1, QPointer<MyClass>
>> > c2)
>> > {
>> >   return c1->comesBefore (c2);
>> > }
>> >
>> > and tried calling qSort as
>> >
>> > qSort (list.begin (), list.end (), objPrecedes);
>> >
>> > but this gives me a compiler error:
>> >
>> > error: no matching function for call to ‘qSort(QList<QPointer<MyClass>
>> >> ::iterator, QList<QPointer<MyClass> >::iterator, <unresolved  
>> overloaded
>> > function type>)’
>> > /usr/include/qt4/QtCore/qalgorithms.h:184: note: candidates are: void
>> > qSort(RandomAccessIterator, RandomAccessIterator, LessThan) [with
>> > RandomAccessIterator = QList<QPointer<MyClass> >::iterator, LessThan =
>> > bool
>> > (ListManager::*)(QPointer<MyClass>, QPointer<MyClass>)]
>> >
>> > Can anyone tell me what I might be doing wrong, here?  I thought about
>> > simply overloading the < operator and not passing the function as an
>> > argument, but my guess is that since this is a list of pointers, it  
>> will
>> > simply compare the values of the pointers rather than try to use < on  
>> the
>> > objects they point to.
>> >
>> > EK
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list