[Interest] Detecting subdirectories in QDir

Oleg Yadrov oleg.yadrov at qt.io
Wed Jul 11 17:59:18 CEST 2018

Ha. I hope I didn't screw up this time.

#include <QCoreApplication>

#include <QDir>

#include <QDebug>

// returns true if 'a' is a subdirectory of 'b'

bool isSubdirectory(QDir a, QDir b)


    while (!a.isRoot()) {

        if (a.absolutePath() == b.absolutePath())

            return true;



    return false;


int main(int argc, char *argv[])


    QCoreApplication app(argc, argv);

    QDir a("/Users/olegyadrov/one/two/three");

    QDir b("/Users/olegyadrov/one/two");

    QDir c("/Users/olegyadrov/one/four");

    qDebug() << isSubdirectory(a, b);

    qDebug() << isSubdirectory(a, c);

    return app.exec();


On Jul 11, 2018, at 10:59 AM, Thiago Macieira <thiago.macieira at intel.com<mailto:thiago.macieira at intel.com>> wrote:

On Wednesday, 11 July 2018 07:44:46 PDT Oleg Yadrov wrote:
   qDebug() << a.absolutePath().startsWith(b.absolutePath());

   qDebug() << a.absolutePath().startsWith(c.absolutePath());

Except that this has a bug (which we had in Qt too): these two directories are
considered to be parent-child, when they're not


And since we're talking about Windows, you also need to use case-insensitive
Thiago Macieira - thiago.macieira (AT) intel.com<http://intel.com>
 Software Architect - Intel Open Source Technology Center

Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180711/46bd810e/attachment.html>

More information about the Interest mailing list