[Qt-interest] Using qSort with lists of QPointers

Rohan McGovern rohan.mcgovern at nokia.com
Mon Sep 6 00:37:36 CEST 2010


Ellen Kestrel said:
> 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>)]

objPrecedes should be a static function.  At the moment you've made it a
member function; that means there's a hidden third parameter which needs
to be passed to each call of the function - the value of `this' -
and qSort wants a function which takes only two parameters.
-- 
Rohan McGovern
QA Engineer
Qt Development Frameworks, Nokia



More information about the Qt-interest-old mailing list