Important: Please read the Qt Code of Conduct -

qSort a QList<struct> by QTime

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

    alt text

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

    alt text
    I need something like:

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

  • Moderators

    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;});

  • @J.Hilk You are really something!

Log in to reply