Results 1 to 14 of 14

Thread: how to sort a list by one of its items ?! [solved]

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by tbscope View Post
    As Zlatomir says

    This is your clue:
    Qt Code:
    1. <unresolved overloaded function type>
    To copy to clipboard, switch view to plain text mode 
    if you mean defining it in my header file , yeah I defined it in my .h file

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to sort a list by one of its items ?!

    No, you should define that in the .cpp file, don't define functions in the .h file, you might get multiple-definitions errors if you include the header in many .cpp files.
    In the header files you should put only declarations, not definitions...

    I made a simple example, the code works, you should play with where you put the declarations/definitions, this is the little test i made:
    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. struct WordItem {
    5. WordItem(int i) : count(i) {}
    6. int count;
    7. };
    8.  
    9. bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
    10. {
    11. return w1.count > w2.count;
    12. }
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QList<WordItem> list;
    17.  
    18. list.append(10);
    19. list.append(11);
    20. list.append(1);
    21.  
    22. qStableSort(list.begin(),list.end(),compareWordItem);
    23.  
    24. for(QList<WordItem>::iterator i = list.begin(); i!= list.end(); ++i)
    25. qDebug() << (*i).count;
    26.  
    27. std::cin.get();
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Zlatomir for this useful post:

    gbmtoday (2nd January 2011)

  4. #3
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    No I meant I declared it in my .h and defined it .cpp ( sorry ! bad English )

    I don't get it , my code looks exactly as yours except that mine is in a class .

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by gbmtoday View Post
    except that mine is in a class .
    Don't forget the namespace!

  6. #5
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    finally I figured what is the problem .

    I should have defined it as a static function.
    in .h :
    Qt Code:
    1. static bool compareWordItem ( const WordItem &w1 , const WordItem &w2 );
    To copy to clipboard, switch view to plain text mode 

    thanks guys

Similar Threads

  1. Replies: 1
    Last Post: 29th November 2010, 17:22
  2. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 13:06
  3. Items in QListView should sort on Header Click
    By vinnu in forum Qt Programming
    Replies: 14
    Last Post: 10th November 2006, 12:49
  4. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 19:08
  5. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 19:43

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.