Results 1 to 2 of 2

Thread: why doesn't qSort work when a QList hosts a pointertype?

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default why doesn't qSort work when a QList hosts a pointertype?

    Hi, I have a QList of pointers and would like to sort them. But it doesn't work because QList seems to only operator on Object not on pointers. It probably boils down to the fact that qSort simply compares the two Types with the < operator and thus ends up comparing the pointer addresses in my case instead of the real value. Maybe I should pass a function to qSort where I compare the values of the types instead. Here is a simple example to demonstrate what I mean (works fine on stack objects but fails if the objects are created on the heap):

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class X
    4. {
    5. public:
    6. X(int value) : m_value(value) { }
    7. bool operator<(const X& x) const { return m_value < x; }
    8. operator int() const { return m_value; }
    9. private:
    10. int m_value;
    11. };
    12.  
    13. int main(int argc, char** argv)
    14. {
    15. QApplication app(argc, argv);
    16.  
    17. qDebug() << "----------- list of objects -------------";
    18. QList<X> list1;
    19. list1 << X(7) << X(18) << X(-5) << X(4);
    20. qSort(list1.begin(), list1.end());
    21. foreach(X x, list1)
    22. qDebug() << x;
    23.  
    24. qDebug() << "------------ list of pointers --------------";
    25. QList<X*> list2;
    26. list2 << new X(1) << new X(3) << new X(0) << new X(6);
    27. qSort(list2.begin(), list2.end());
    28. foreach(X* x, list2)
    29. qDebug() << *x;
    30. }
    To copy to clipboard, switch view to plain text mode 
    Thanx in advance
    Last edited by momesana; 4th October 2009 at 05:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why doesn't qSort work when a QList hosts a pointertype?

    Ok, providing a lessThan function that takes two Pointers as parameters solved it :-). Applied to the above example it would look like that:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class X
    4. {
    5. public:
    6. X(int value) : m_value(value) { }
    7. bool operator<(const X& x) const { return m_value < x; }
    8. operator int() const { return m_value; }
    9. private:
    10. int m_value;
    11. };
    12.  
    13. bool lessThan(const X* x1, const X* x2) { return *x1 < *x2; }
    14.  
    15. int main(int argc, char** argv)
    16. {
    17. QApplication app(argc, argv);
    18.  
    19. qDebug() << "----------- list of objects -------------";
    20. QList<X> list1;
    21. list1 << X(7) << X(18) << X(-5) << X(4);
    22. qSort(list1.begin(), list1.end());
    23. foreach(X x, list1)
    24. qDebug() << x;
    25.  
    26. qDebug() << "------------ list of pointers --------------";
    27. QList<X*> list2;
    28. list2 << new X(1) << new X(3) << new X(0) << new X(6);
    29. qSort(list2.begin(), list2.end(), lessThan);
    30. foreach(X* x, list2)
    31. qDebug() << *x;
    32. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55
  2. qSort doesn't work with member function
    By ber_44 in forum Qt Programming
    Replies: 10
    Last Post: 2nd June 2007, 13:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.