Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. HasNext() returns true when the Qstring passed to QDirIterator is null
Qt 6.12 LTS is out! See what's new in the release blog

HasNext() returns true when the Qstring passed to QDirIterator is null

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Using version 4.7.4 and the following code:
    @
    QDirIterator Obj(QString((*itr).c_str()), QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden, QDirIterator::Subdirectories);
    while (Obj.hasNext())
    {
    Obj.next();

    if(Obj.fileInfo().isDir())
    {
    std::string tempDir = Obj.filePath().toStdString() + "/";
    tempList.push_back(tempDir);
    }
    }
    @
    Obj.hasNext() returns false for a Qstring of "". However in 4.8.6 Obj.hasNext() returns true for a QString of "". Is this a bug? Is there anyway to make 4.8.6 work like 4.7.4?

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi All

      In the end I coded a work around. I now check the Qstring and if it is empty then I do not call QDirIterator. What I really need to know is did I find a bug in 4.8.6 or a new feature?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Where does "QString((*itr).c_str())" come from ?

        I just tested with 4.8.6 replacing QString((*itr).c_str()) with QString("") and it does work as expected.

        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
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          What is the "expected behavior" for iterating an empty string anyway? It's obviously not a valid directory, so what would you expert? The result probably is undefined. Or does it implicitly iterate the current directory ???

          (In the later case, the result depends on whatever happens to be your "current" directory - which is never a good idea)

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Here is the entire code including my fix:
            @void Demo::FindAllSubDirs(PathList &list)
            {
            // A container to store the subdirectory paths
            PathList tempList;

            for (PathList::iterator itr = list.begin(); itr != list.end(); ++itr)
            {
            tempList.push_back(*itr);
            if(!(QString((*itr).c_str()).isEmpty()))
            {
            QDirIterator Obj(QString((*itr).c_str()), QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden, QDirIterator::Subdirectories);
            while (Obj.hasNext())
            {
            Obj.next();

            if(Obj.fileInfo().isDir()) 
            {
             std::string tempDir = Obj.filePath().toStdString() + "/";
             tempList.push_back(tempDir);
            }
            

            }
            }
            }

            list.clear();
            list = tempList;
            }
            @
            I also replace the QDirIterator line with this line as suggested and YES it does fail as Obj.hasNext() returns true and should return false:
            @QDirIterator Obj(QString(""), QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden, QDirIterator::Subdirectories);@

            The list requires a null as the first entry in the list.

            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
            • Unsolved