Qt Forum

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

    QFileInfoList sorting using QDir::Time

    General and Desktop
    5
    10
    13231
    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.
    • H
      heliosbird last edited by

      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

      1 Reply Last reply Reply Quote 0
      • A
        Ah_Kin last edited by

        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>

        noone knows

        1 Reply Last reply Reply Quote 0
        • T
          tucnak last edited by

          [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.

          1 Reply Last reply Reply Quote 0
          • R
            rafa2210 last edited by

            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).

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • A
                Ah_Kin last edited by

                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.

                noone knows

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  What report ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • A
                    Ah_Kin last edited by

                    the one you talked about.

                    noone knows

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply Reply Quote 0
                      • R
                        rafa2210 last edited by

                        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

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