Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileInfoList sorting using QDir::Time



  • Hello All!

    I am trying to find the most recently created directory using QDir and QFileInfoList. I create the QFileInfoList using the filters QDirs::Dirs and QDir::NoDotAndDotDot, then sort using QDir::Time, shown in my code below. This works most of the time, but it sometimes sorts incorrectly, i.e. the second most recent is at index 0, not the most recent. I confirmed this behaviour with a qDebug printout, shown below my code. This printout shows the date and time.

    @QDir loggingDir(pathString);

    QFileInfoList dirList = loggingDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);

    for(int i = 0; i < dirList.size(); i++)
    {
    qDebug() << dirList.at(i).absoluteFilePath() << dirList.at(i).lastModified();
    }
    @

    @"C:/log/2012_10_23_15_03_20_183" QDateTime("Tue Oct 23 15:03:20 2012")
    "C:/log/2012_10_23_15_03_20_886" QDateTime("Tue Oct 23 15:03:21 2012")
    "C:/log/2012_10_23_15_03_18_734" QDateTime("Tue Oct 23 15:03:19 2012")
    "C:/log/2012_10_23_15_03_19_639" QDateTime("Tue Oct 23 15:03:19 2012")
    "C:/log/2012_10_23_15_03_11_623" QDateTime("Tue Oct 23 15:03:12 2012")
    "C:/log/2012_10_23_15_02_59_313" QDateTime("Tue Oct 23 15:02:59 2012")
    "C:/log/2012_10_23_15_02_57_815" QDateTime("Tue Oct 23 15:02:58 2012") @

    Am I missing something? Is this a bug? I assume I can get around this by manually sorting through the directories and comparing the time, but I would like to see what I've done wrong using this method.

    Just to clarify, the 2012_10_23_xx_xx_xx_xxx are directories, not files.

    I am using Qt 4.8.2 32 bit on a windows 7 machine (64 bit). Thanks!

    AB



  • Qdebug "forget" many files for me. I'm gonna try to display the list on any widget, i'll get back here as so as i can.

    Regarding the QFileInfoList, Qt isn't helping much with

    bq. typedef QFileInfoList. Synonym for QList<QFileInfo>



  • [quote author="heliosbird" date="1351030512"]
    Am I missing something? Is this a bug?
    [/quote]

    Hi, ~heliosbird!

    It looks like your code is correct. I'll review this part of Qt source tomorrow and if there is a bug..I'll make you noticed.

    Thanks for an issue.



  • I know this is quite an old post, but i am using QT5.1 and this problem seems to continue to be present. Has anyone found a way around it? (other than manually sorting the list).


  • Lifetime Qt Champion

    Hi,

    If it's something repeatable then it might be a bug. The best thing to do is to check the "bug report system":http://bugreports.qt-project.org to see if it's something known. If not please consider opening a new report providing a minimal compilable example that shows the behavior



  • In case this is lasting two more years before the report is getting read, here is a "patch" I'm using :

    the .h includes :
    QFileInfoList* m_list;
    QDir* m_dir;

    @
    m_list = new QFileInfoList(m_dir->entryInfoList());
    QStringList fileStringList;

    for (int i = 0; i < m_list->size(); i++)
        fileStringList << m_list->at(i).fileName().remove(".mp3");
    
    QMap<int,QString> mapFichiers;
    for (int i = 0; i < fileStringList.size(); ++i)
        {
    

    mapFichiers.insert(m_list->at(i).lastModified().msecsTo(QDateTime(QDate(1,1,1),QTime(1,1)))*-1,fileStringList.at(i));
    }
    @

    to get the name of each file without the extension (here .mp3) sorted by date. That might help.


  • Lifetime Qt Champion

    What report ?



  • the one you talked about.


  • Lifetime Qt Champion

    I don't know if this is already reported, I just proposed to open one if it's not the case



  • It turns out that the problem is getting the sorted by the millisecond (what i need). I thought it was possible to get the lastModified time to the millisecond, but apparently not. So i supose it is not a bug of QT.

    thank you for your help


Log in to reply