Results 1 to 11 of 11

Thread: qSort doesn't work with member function

  1. #1
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default qSort doesn't work with member function

    I need to pass to qSort() a special lessThan function that works on class members.
    Looks like that TT did not know about this C++ limitation when they designed qSort(): http://www.parashift.com/c++-faq-lit....html#faq-33.2

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qSort doesn't work with member function

    What do you mean?
    That article was not referring to Qt's signal and slots.
    Do you need to pass a pointer to a function to a signal?
    That can be done.

    Or do you need to just pass a pointer to class member function?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qSort doesn't work with member function

    I don't really understand... You can just overload the operator<() function or method instead. I haven't tried the option with a function but I don't see any reason why it shouldn't work.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qSort doesn't work with member function

    void qSort ( RandomAccessIterator begin, RandomAccessIterator end )
    Sorts the items in range [begin, end) in ascending order using the heap sort algorithm.
    Example:
    QList<int> list;
    list << 33 << 12 << 68 << 6 << 12;
    qSort(list.begin(), list.end());
    // list: [ 6, 12, 12, 33, 68 ]
    The sort algorithm is efficient on large data sets. It operates in linear-logarithmic time, O(n log n).
    This function requires the item type (in the example above, int) to implement operator<().
    If of two items neither is less than the other, the items are taken to be equal. It is then undefined which one of the two items will appear before the other after the sort.
    As you can see, you don't need any "special" function that operates on member function.
    Just put the elements you need to sort in a QList or something similar and implement for that class( items class ) operator <, or operator >, whatever you need, and qSort will take care of the rest.

    Regards

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qSort doesn't work with member function

    Quote from qt docs for qSort()
    void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
    This is an overloaded member function, provided for convenience.
    Uses the lessThan function instead of operator<() to compare the items.
    For example, here's how to sort the strings in a QStringList in case-insensitive alphabetical order:
    bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
    {
    return s1.toLower() < s2.toLower();
    }

    int doSomething()
    {
    QStringList list;
    list << "AlPha" << "beTA" << "gamma" << "DELTA";
    qSort(list.begin(), list.end(), caseInsensitiveLessThan);
    // list: [ "AlPha", "beTA", "DELTA", "gamma" ]
    }
    To sort values in reverse order, pass qGreater<T>() as the lessThan parameter. For example:
    QList<int> list;
    list << 33 << 12 << 68 << 6 << 12;
    qSort(list.begin(), list.end(), qGreater<int>());
    // list: [ 68, 33, 12, 12, 6 ]
    What else do you want?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #6
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qSort doesn't work with member function

    Gopala, caseInsensitiveLessThan() is a global function and cannot access class members.
    Furthermore, I need to switch between several "less than" functions (depending on user input) so I can't do operator overloading.
    I guess QSortFilterProxyModel::lessThan() would do it, but so far I didn't have luck with it.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qSort doesn't work with member function

    You can overload operator < and let it behave differently according to user input. Just add a few flags to your class and do the sorting according to those flags.

  8. #8
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qSort doesn't work with member function

    OK, thank you, that would be doable.
    Meanwhile I made the proxymodel work,just had to call
    proxyModel->sort(0, Qt::AscendingOrder);
    after reimplementing lessThan().

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qSort doesn't work with member function

    Quote Originally Posted by ber_44 View Post
    Gopala, caseInsensitiveLessThan() is a global function and cannot access class members.
    Furthermore, I need to switch between several "less than" functions (depending on user input) so I can't do operator overloading.
    I guess QSortFilterProxyModel::lessThan() would do it, but so far I didn't have luck with it.
    You can access public members from a class right? If you want to base the sort on private member, you can add a public member function and then access them.

    You can have different functions (global or static member) and pass the required one to the qSort function

    Qt Code:
    1. bool lessThan1(const Type& t1, const Type& t2)
    2. {
    3. return t1.a() < t2.a(); //or what ever
    4. }
    5.  
    6. bool lessThan2(const Type& t1, const Type& t2)
    7. {
    8. return t2 < t1;
    9. }
    10.  
    11. if(userInput == 1)
    12. qSort(list.begin(), list.end(),lessThan1);
    13. else
    14. qSort(list.begin(), list.end(),lessThan2);
    To copy to clipboard, switch view to plain text mode 

    You can also use pointer to function and set the pointer to whatever function you want and pass it to qSort()
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qSort doesn't work with member function

    Quote Originally Posted by ber_44 View Post
    OK, thank you, that would be doable.
    Meanwhile I made the proxymodel work,just had to call
    proxyModel->sort(0, Qt::AscendingOrder);
    after reimplementing lessThan().
    Oh ok fine. You just wanted to sort. Since you were blaming TT for design issue I thought you wanted to use qSort in your code rather than sort them.
    Sorry for misunderstanding!
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qSort doesn't work with member function

    Quote Originally Posted by ber_44 View Post
    Gopala, caseInsensitiveLessThan() is a global function and cannot access class members.
    If you want to access private data, you can declare the sorting function as a friend. Or better yet implement public methods in your class that do different types of comparisons and just call them from within the lessThan implementation. Something like:

    Qt Code:
    1. bool MyLessThan(const obj &o1, const obj &o2){
    2. return o1.isLessThan(o2, Qt::CaseInsensitive);
    3. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  5. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 02:44

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.