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



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



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


  • Lifetime Qt Champion

    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.



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.