Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qSort a QList<struct> by QTime
Forum Updated to NodeBB v4.3 + New Features

qSort a QList<struct> by QTime

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    Scene: a line of lyric contains more than one time tag.

    alt text

    struct Lyric{
            QTime time;
            QString sentence;
    };
    QList<Lyric> lyrics;
    ......
    lyrics.clear();
    QStringList line = slyric.split("\n");
    for(int i=0; i<line.size(); i++){
        if(line.at(i).contains("]")){
            QStringList SL = line.at(i).split("]");
            for(int j=0; j<SL.length()-1; j++){
                Lyric lyric;
                QString stime = SL.at(j).mid(1);
                if((stime.length() - stime.indexOf(".")) == 3) stime += "0";
                lyric.time = QTime::fromString(stime, "mm:ss.zzz");
                lyric.sentence = SL.at(SL.length()-1);
                lyrics.append(lyric);
            }
        }
    }
    for(int i=0; i<lyrics.size(); i++){
        textBrowser->insertPlainText(lyrics.at(i).time.toString("mm:ss.zzz") + " " + lyrics.at(i).sentence + "\n");
    }
    

    alt text
    I need something like:

    qSort(lyrics.begin(), lyrics.end(), Lyric.time);
    

    https://github.com/sonichy

    RatzzR 1 Reply Last reply
    0
    • sonichyS sonichy

      Scene: a line of lyric contains more than one time tag.

      alt text

      struct Lyric{
              QTime time;
              QString sentence;
      };
      QList<Lyric> lyrics;
      ......
      lyrics.clear();
      QStringList line = slyric.split("\n");
      for(int i=0; i<line.size(); i++){
          if(line.at(i).contains("]")){
              QStringList SL = line.at(i).split("]");
              for(int j=0; j<SL.length()-1; j++){
                  Lyric lyric;
                  QString stime = SL.at(j).mid(1);
                  if((stime.length() - stime.indexOf(".")) == 3) stime += "0";
                  lyric.time = QTime::fromString(stime, "mm:ss.zzz");
                  lyric.sentence = SL.at(SL.length()-1);
                  lyrics.append(lyric);
              }
          }
      }
      for(int i=0; i<lyrics.size(); i++){
          textBrowser->insertPlainText(lyrics.at(i).time.toString("mm:ss.zzz") + " " + lyrics.at(i).sentence + "\n");
      }
      

      alt text
      I need something like:

      qSort(lyrics.begin(), lyrics.end(), Lyric.time);
      
      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      @sonichy said in qSort a QList<struct> by QTime:

      qSort

      U mean this http://doc.qt.io/archives/qt-4.8/qtalgorithms.html#qSort-2 ?

      --Alles ist gut.

      1 Reply Last reply
      3
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by VRonin
        #3

        you can use std::sort and define your own function /lambda to sort by.

        something like this (untested)

        QList<Lyric> lyrics;
        std::sort(lyrics.begin(), lyrics.end(), [](const Lyric& a, const Lyric& b)->bool{return a.time < b.time;});
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        sonichyS 1 Reply Last reply
        9
        • J.HilkJ J.Hilk

          you can use std::sort and define your own function /lambda to sort by.

          something like this (untested)

          QList<Lyric> lyrics;
          std::sort(lyrics.begin(), lyrics.end(), [](const Lyric& a, const Lyric& b)->bool{return a.time < b.time;});
          
          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4

          @J.Hilk You are really something!

          https://github.com/sonichy

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved