Results 1 to 3 of 3

Thread: Sorting a QList

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Sorting a QList

    Hallo,

    in my app the user has the possibility to add a station to a QListWidget. This works very well. After adding the station the list has to be updated. The problem, the QListWidget reads the stations and its frequency from a txt file.

    So i need to sort the stations in the txt file not in the QListWidget, as I use the row index to tune to the station.

    How can I sort the output before it is saved to the txt file?

    The strings to campore look something like:

    Jump,105600000
    Station 1 93 MHz,93000000
    man station@ 101.2MHz,101200000

    Qt Code:
    1. ui->btn_rename->setVisible(true);
    2.  
    3. QString add_station = ui->ln_man_tune->text();
    4. float add_station_float = add_station.toFloat();
    5. int multi = add_station_float * 1000000;
    6. QString station_conv_string = (QString::number(multi));
    7.  
    8. if(!add_station.isEmpty()){
    9. QFile out_tmp("../tmp.txt");
    10. QFile file_fm(path_fm);
    11.  
    12. if(!file_fm.open(QFile::ReadOnly | QFile::Text)){
    13. return;
    14. }
    15.  
    16. if(!out_tmp.open(QFile::WriteOnly | QFile::Text)){
    17. return;
    18. }
    19.  
    20. QTextStream in_file_fm(&file_fm);
    21. file_fm.resize(0);
    22. QTextStream out(&out_tmp);
    23. QStringList unsort_scan_fm;
    24.  
    25. while(!in_file_fm.atEnd()){
    26. QString line = in_file_fm.readLine();
    27.  
    28. QString outline = line;
    29.  
    30. unsort_scan_fm.append(outline);
    31. }
    32.  
    33. if(add_station.contains(",")){
    34. add_station = add_station.replace(",", ".");
    35. }
    36. if(!add_station.contains(".")){
    37. add_station = add_station.append(".0");
    38. }
    39.  
    40. //add new station at end of file
    41. QString outline = "man station@ " + add_station + "MHz," + station_conv_string;
    42.  
    43. unsort_scan_fm.append(outline);
    44. qDebug() << "before sort: :" << unsort_scan_fm;
    45.  
    46. std::sort(unsort_scan_fm.begin(), unsort_scan_fm.end());
    47.  
    48. qDebug() << "after sort: :" << unsort_scan_fm;
    49.  
    50. foreach(QString after_sort, unsort_scan_fm){
    51. out << after_sort << "\n";
    52. }
    53.  
    54. file_fm.close();
    55. out_tmp.flush();
    56. out_tmp.close();
    57.  
    58. file_fm.remove();
    59. out_tmp.rename(path_fm);
    60.  
    61. ui->ls_fm->clear();
    62.  
    63. MainWindow::fill_list();
    64. MainWindow::fm_list();
    65. }
    To copy to clipboard, switch view to plain text mode 

    Debug before sort and after sort are the same:

    Qt Code:
    1. after sort: : ("Jump,105600000", "Station 1 93 MHz,93000000", "Station 3 98.1 MHz,98100000", "Station 4 99.9 MHz,99900000", "Top40,94800000", "any station,107600000", "man station@ 100.2MHz,100200000", "man station@ 101.2MHz,101200000", "man station@ 101.3MHz,101300000", "man station@ 101.3MHz,101300000", "man station@ 104.6MHz,104600000", "man station@ 105.3MHz,105300000", "man station@ 105.4MHz,105400000", "man station@ 111.1MHz,111100000", "man station@ 96.8MHz,96800000", "man station@ 97.8MHz,97800000", "man station@ 99.9MHz,99900000", "zulu station,100700000")
    To copy to clipboard, switch view to plain text mode 
    Last edited by vitalic; 28th April 2020 at 19:15.

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. sorting QList<double>
    By timmu in forum Qt Programming
    Replies: 7
    Last Post: 24th August 2012, 16:35
  3. sorting QList<float>
    By timmu in forum Newbie
    Replies: 2
    Last Post: 23rd January 2012, 17:16
  4. sorting static QList<MyClass> list with qSort
    By oruccim in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2010, 16:13
  5. Replies: 5
    Last Post: 18th December 2008, 13:58

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.