Results 1 to 4 of 4

Thread: error with qsort

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default error with qsort

    hi
    i had pblm using qsort to sort arrays. i used Qvector but it didnt wrk. plz help me with this
    here is my code:
    Qt Code:
    1. public:
    2. explicit MainWindow(QWidget *parent = 0);
    3. QVector <double> temp_array;
    4. int comp(const void * a,const void * b);
    5. ~MainWindow();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. qStablesort(temp_array, temp_array.size(), sizeof(double), int comp);
    7.  
    8. }
    9. int MainWindow:: comp(const void * a,const void * b)
    10. {
    11.  
    12. if (*(double*)a > *(double*)b) return 1;
    13. else if (*(double*)a < *(double*)b) return -1;
    14. else return 0; ;
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: error with qsort

    Why not use the standard features of Qt : qSort( Container & container ) or qStableSort( Container & container ) ?

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

    vinaysagar (15th July 2014)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: error with qsort

    Quote Originally Posted by vinaysagar View Post
    hi
    i had pblm using qsort to sort arrays. i used Qvector but it didnt wrk. plz help me with this
    here is my code:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. qStablesort(temp_array, temp_array.size(), sizeof(double), int comp);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    "it didnt wrk" is not very informative. What were you expecting? What did you get instead?

    Your call to qStablesort() is syntactically incorrect (invalid C++, nothing to do with Qt). In addition, you pass 4 arguments, while no variants of this method with more than 3 parameters seem to exist. Here is a link to the documentation of qStablesort(): http://qt-project.org/doc/qt-4.8/qta...ml#qStableSort. By the way, as implied by Lesiok, you do not need a custom comparison function if all it does is mimic the < operator; and your (non static!) function does not have the correct prototype anyway (this is C++, not C's qsort).

    From Qt 5.0 on, these generic algorithms are deprecated. I suggest you call the standard C++ equivalent instead: http://en.cppreference.com/w/cpp/algorithm/stable_sort.

  5. The following user says thank you to yeye_olive for this useful post:

    vinaysagar (15th July 2014)

  6. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: error with qsort

    It might be more useful if the vector actually contained some data also.

Similar Threads

  1. Don't understand qSort behaviour....
    By Qtonimo in forum Qt Programming
    Replies: 12
    Last Post: 2nd August 2012, 16:04
  2. QSort
    By skumar7777 in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2008, 06:18
  3. qSort with lessThan
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 19th August 2008, 18:14
  4. qSort() problem
    By darksaga in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 11:47
  5. [Qt4] QSORT
    By jane in forum Qt Programming
    Replies: 6
    Last Post: 24th May 2006, 23:38

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.