Ok, providing a lessThan function that takes two Pointers as parameters solved it :-). Applied to the above example it would look like that:
#include <QtGui>
class X
{
public:
X(int value) : m_value(value) { }
bool operator<(const X& x) const { return m_value < x; }
operator int() const { return m_value; }
private:
int m_value;
};
bool lessThan(const X* x1, const X* x2) { return *x1 < *x2; }
int main(int argc, char** argv)
{
qDebug() << "----------- list of objects -------------";
QList<X> list1;
list1 << X(7) << X(18) << X(-5) << X(4);
qSort(list1.begin(), list1.end());
foreach(X x, list1)
qDebug() << x;
qDebug() << "------------ list of pointers --------------";
QList<X*> list2;
list2 << new X(1) << new X(3) << new X(0) << new X(6);
qSort(list2.begin(), list2.end(), lessThan);
foreach(X* x, list2)
qDebug() << *x;
}
#include <QtGui>
class X
{
public:
X(int value) : m_value(value) { }
bool operator<(const X& x) const { return m_value < x; }
operator int() const { return m_value; }
private:
int m_value;
};
bool lessThan(const X* x1, const X* x2) { return *x1 < *x2; }
int main(int argc, char** argv)
{
QApplication app(argc, argv);
qDebug() << "----------- list of objects -------------";
QList<X> list1;
list1 << X(7) << X(18) << X(-5) << X(4);
qSort(list1.begin(), list1.end());
foreach(X x, list1)
qDebug() << x;
qDebug() << "------------ list of pointers --------------";
QList<X*> list2;
list2 << new X(1) << new X(3) << new X(0) << new X(6);
qSort(list2.begin(), list2.end(), lessThan);
foreach(X* x, list2)
qDebug() << *x;
}
To copy to clipboard, switch view to plain text mode
Bookmarks