QFileInfo: how to have correct fileName() from a directory include spaces (and/or specials chars) ?

  • I have some problem again with use of Qt5:

    i have a directory like this: /AAA/BBB/de fg (120)
    and then, i have a QUrl with it and use the QFileInfo for try to sort/populate a tree...
    with this directory, it is impossible to have a fileName() (fileName is empty... "")
    Also, i try to use heavy QRegExp to find a solution for ne able to read it...
    but again... the regex works in a tools for check Qt regex (who are not same than perl regex...) but not inside my application.
    here is the part of my code:
    if (file_info.isDir()) { // directory
    QString absolute_dir = file_info.absoluteFilePath();
    QString relative_dir = file_info.canonicalPath() + "/"; // i use canocicalPath, because with this directory who include spaces, there is no way to use absolutPath who give exactly same than absolutFilePath...
    if(dir_items.contains(relative_dir)) {
    QRegExp regex("\/.\/([\w\s_()&@-])$"); // i escape "" but maybe it as to be more heavy to use a regex ? here, i try to do the job QFileInfo ius not able to do by regex: catch the fileName() of the directory.
    QString short_dir = absolute_dir;
    short_dir.replace(regex, "\1"); // here, i hope to have the fileName() include spaces, and some special chars a dir can bring inside with linux (i really don't care window OS ability)
    rep = new QStandardItem(short_dir);
    new_row.insert(0, rep);
    dir_items[relative_dir]->appendRow(new_row); }
    else {
    rep = new QStandardItem(absolute_dir);
    new_row.insert(0, rep);
    this->appendRow(new_row); }
    dir_items.insert( absolute_dir, rep);

    is it normal that QFileInfo has this reaction (and then... how to ask him to do this job fine ?) or is it a bug ?
    could someone know how to do in this situation (please... not answer to not include spaces inside directory name... linux can do it easy and this not have to be a problem to do it) please ?

  • I haven't seen problems with QFileInfo or QDir when spaces are included in the name. For special characters that need to be modified I would use the QString::replace() function.

    I am not sure what you are trying to accomplish but it appears that you want to create a tree structure (QStandardItem) based on the directory hierarchy.

    Maybe something that processes the directory recursively looking for the files at the bottom of the directory tree would be easier?

  • yes, it is exactly what i done... the code is not full here, but like you tell, i done it well. Also, all works fine only for directories where are no spaces inside the name.
    In the debugger, i also check all entries and see that two methods not works well when spaces occurs: fileName() and absolutePath() (this end one give me back same that absoluteFilePath() without the last "/"...

    i arrived to find a solution with QRegExp... but that seems that QfileInfo not works fine in this situation...

    i'm under archlinux 64bits with Qt-5.4 and compil with gcc.

  • also... maybe this could be an information for test (but maybe not):
    My file system partition is a BTRFS (and not ext4).

    many different test... allways the same problem for the same directories includes spaces inside his name.

    When the QFileInfo is a file instead of a directroy, spaces in the file name is not a problem. So the problem is ONLY for directory type.

    Could someone try to test QFileInfo::fileName() and ::absolutPath() with linux and btrfs file system for directory name include spaces and "()" chars inside ?

  • ok, i never find why this happen and it is not something normal from this Qt QFileInfo.
    SO i consider it bugged and not possible to use with linux 64 bits and btrfs file system (because some other tell me this works for them... but no one test in this configuration).
    So... i will post on bug report.


  • Hi,

     Its been a long time since I used QDir and QFileInfo, but I have in Qt5.x and they work fine on Ubuntu 64 (however I used EXT4). If I read the docs right, I don't think fileName() will get populated if isDir is true since it is a directory and not a file (which is probably why it works when you use a actual file). 

    Perhaps you may want to use dirName instead. I do have some working code on my other laptop that I can post for you for a media center I've been working on that parses directories recursivley for files if you need further assistance.

  • Chrisw01:

    thanks for your answer.

    like i tell you, some other peoples tell me allready the same with ext4 system file, but it would be appreciate to really try with btrfs (just partition a little part of a disk with it... you can do it with ubuntu 64 also and you can do it easy without lake more than 10 minutes of time... maybe the time for redaction here...).

    3 the doc is not easy to read "well" without know allready Qt,and/or when we are beginners (that i said many times, but i know that write a doc and provide usualls and precise explications is not so easy that the writers seems to think... it is a special job i think)... so i thank same you before on this subject,

    but the fact is that fileNeme() method will give you back the last part of your directory name in case of directory. WHY ? the reason is that under linux, all is file (directory is also a particular type of file... symbolic link too.. etc...), some guys teach me that on irc channel of Qt, so... the name of this specific "directory container/marker" file is his end (after last "/") name.

    4 sure... isDir() is true when it is a direrctory, and false when it is a file (no problem with this... works perfectly).
    5 also, i tell you that absolutPath() has problem in the same case of directory include spaces for btrs file system...

    Chrisw01: you can just go under your debugger and do a test (and i'm sure for this long time you use QDir and QFileInfo, it would not be a bad idea to go looking inside the variables at run time what's happen)... and see by your self that... the doc is difficult to understand precisely and fileName() definitly has to give you back something, ablosutePath() the rest of: [absoluteFilePath() - fileName()].

    but please Chrisw01, if you really want to help to point maybe a bug (or show me that there is a local problem on my installation... because this problem is not something normal), could you please do it by test with same situation ? I read the debugger and the content of this DFileInfo for different files / directories before point this strange fact.
    So... try to do it with a btrfs system file and some directories named with spaces inside and other "(", ")", etc...

    i appreciate again your answer, but i would really appreciate more also if you really want to try BEFORE boubt without any argument (for me, be an old user of QFileInfo is not a validate argument too).
    I'm also happy that a beginner like me could teach an old QFileInfo user like you that fileName() give you back something in case of directory reading. I hope you will appreciate too (that is something magic about the communication).

    thanks again... i wait for you tell me what is the result of your tests with btrfs system file in same conditions. If also you think about some other thinks could happen or perturbate the QFileInfo class of Qt, tell me...i maybe forget or not see something.

Log in to reply

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