[Interest] Detecting subdirectories in QDir

Mårten Nordheim marten.nordheim at qt.io
Thu Jul 12 09:30:48 CEST 2018


Does it work properly if 'b' is root?

On 11.07.2018 17:59, Oleg Yadrov wrote:
> Ha. I hope I didn't screw up this time.
> 
> #include<QCoreApplication>
> 
> #include<QDir>
> 
> #include<QDebug>
> 
> 
> //returnstrueif'a'isasubdirectoryof'b'
> 
> boolisSubdirectory(QDira,QDirb)
> 
> {
> 
> while(!a.isRoot()){
> 
> if(a.absolutePath()==b.absolutePath())
> 
> returntrue;
> 
> a.cdUp();
> 
> }
> 
> returnfalse;
> 
> }
> 
> 
> intmain(intargc,char*argv[])
> 
> {
> 
> QCoreApplicationapp(argc,argv);
> 
> 
> QDira("/Users/olegyadrov/one/two/three");
> 
> QDirb("/Users/olegyadrov/one/two");
> 
> QDirc("/Users/olegyadrov/one/four");
> 
> 
> qDebug()<<isSubdirectory(a,b);
> 
> qDebug()<<isSubdirectory(a,c);
> 
> 
> returnapp.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
>>
>> C:\One\TwoAndTwenty
>> C:\One\Two
>>
>> And since we're talking about Windows, you also need to use 
>> case-insensitive
>> comparison.
>> -- 
>> 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>
>> http://lists.qt-project.org/mailman/listinfo/interest
> 
> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> 



More information about the Interest mailing list