Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved qSort a QList<struct> by QTime

    General and Desktop
    3
    4
    868
    Loading More Posts
    • 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.
    • sonichy
      sonichy last edited by

      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

      Ratzz 1 Reply Last reply Reply Quote 0
      • Ratzz
        Ratzz @sonichy last edited by

        @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 Reply Quote 3
        • J.Hilk
          J.Hilk Moderators last edited by VRonin

          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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          sonichy 1 Reply Last reply Reply Quote 9
          • sonichy
            sonichy @J.Hilk last edited by

            @J.Hilk You are really something!

            https://github.com/sonichy

            1 Reply Last reply Reply Quote 0
            • First post
              Last post