Results 1 to 6 of 6

Thread: qSort() problem

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question qSort() problem

    hi all,

    I have a strange prob to sort my QList with the qSort() function.

    the class to be sorted looks like:
    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. class MyClass{
    5. public:
    6. MyClass(){}
    7. ~MyClass{}
    8. bool operator<(const MyClass& a) const {
    9. return getBpCount() < a.getBpCount();
    10. }
    11. const float getBpCount(void) const{return /*some calculated float*/;}
    12. }
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    in the class, where I use the class above, I do the following:
    Qt Code:
    1. QList<MyClass*> *list = new QList<MyClass*>;
    2. for(){/*create new MyClasses & add to list*/}
    3.  
    4. //finally
    5. //checked the listorder here1
    6. qSort(*list);
    7. //and here2
    To copy to clipboard, switch view to plain text mode 

    the list should be sorted, but it isn't

    there is some change between here1 & here2 but @ here2 the list isn't oredered in an ascending or descending order, it seems to be random to me

    can anybody tell me what I'm missing or doing wrong???

    regards

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qSort() problem

    The list gets sorted by pointer values. You should store objects (eg. QList<MyClass>) to get it sorted like you want.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qSort() problem

    Oh, and by the way. Actually you can sort pointers too with the help of custom comparator function:
    Qt Code:
    1. bool myClassLessThan(const MyClass* a, const MyClass* b)
    2. {
    3. return a->getBpCount() < b->getBpCount();
    4. }
    5.  
    6. qSort(list->begin(), list->end(), myClassLessThan);
    To copy to clipboard, switch view to plain text mode 

    As a side note, QList is an implicitly shared class so usually it's allocated on stack.
    J-P Nurmi

  4. #4
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: qSort() problem

    ok,
    this explains the "random" order after using the qSort funcion

    did it the way u suggested, and tried to use the lessThan function, but now my compiler starts to bitch around

    if the lessThan function is defined in MyClass, the compiler says:
    error c2065: 'myClassLessThan': unknown identifier
    if I declare lessThan in the class, where i use the qSort function, it says:
    moc_ResultContainer.cpp
    ..\..\qt4\include\QtCore\qalgorithms.h(359) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
    ..\..\qt4\include\QtCore\qalgorithms.h(184): Siehe Verweis auf Instanziierung der kompilierten Funktionsvorlage 'void QAlgorithmsPrivate::qSortHelper<RandomAccessIterat or,T,LessThan>(RandomAccessIterator,RandomAccessIt erator,const T & ,LessThan)'
    with
    [
    RandomAccessIterator=QList<MyClass *>::iterator,
    T=MyClass *,
    LessThan=bool (__thiscall ResultContainer::* )(const MyClass *,const MyClass *)
    ]
    c:\devel\projects\projekt_main_vs\release\../src\Workbench\ResultContainer.h(56): Siehe Verweis auf Instanziierung der kompilierten Funktionsvorlage 'void qSort<QList<T>::iterator,bool(__thiscall ResultContainer::* )(const MyClass *,const MyClass *)>(RandomAccessIterator,RandomAccessIterator,Less Than)'
    with
    [
    T=MyClass *,
    RandomAccessIterator=QList<MyClass *>::iterator,
    LessThan=bool (__thiscall ResultContainer::* )(const MyClass *,const MyClass *)
    ]
    ..\..\qt4\include\QtCore\qalgorithms.h(364) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
    ..\..\qt4\include\QtCore\qalgorithms.h(366) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
    ..\..\qt4\include\QtCore\qalgorithms.h(374) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
    so, what is it now, that I'm missing?
    Last edited by jacek; 17th August 2007 at 23:54. Reason: changed [code] to [quote]

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qSort() problem

    You are passing a function pointer so the function must be
    • either declared as static member function
    • or declared in the global namespace (not a member function)

    For more details, see C++ FAQ LITE: [33] Pointers to member functions.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    darksaga (17th August 2007)

  7. #6
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: qSort() problem

    Quote Originally Posted by jpn View Post
    You are passing a function pointer so the function must be
    • either declared as static member function
    • or declared in the global namespace (not a member function)

    For more details, see C++ FAQ LITE: [33] Pointers to member functions.
    makes sense , though I don't like static & global declared stuff...

    works like a charm now, thanks alot jpn

Similar Threads

  1. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  2. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.