Results 1 to 8 of 8

Thread: QT4/C++ - Problem with QFileInfo

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QT4/C++ - Problem with QFileInfo

    Hello,

    Debian Wheezy 4.0.5
    Qt 4.8.2 - GCC 4.6 or 4.8
    Qt Creator 2.5.0

    list.jpg

    I'm trying to understand QFileInfo.
    The problem I've got is that an entry 'alsamixer' that shows as a directory when I think it
    should be a .desktop file.
    Its quite probable that I don't understand the 'linux' file system or I'm making a mistake.
    Perhaps some one could take a look at the code and tell me where I've gone wrong.

    Regards

    A minimal program that shows this is on dropbox https://www.dropbox.com/sh/dzrqnqu40...A0RpkI48a?dl=0
    Qt Code:
    1. private:
    2. QListWidget *subMenu;
    3. QStringList *fileList[19], *base;
    4. QDir baseDir[19];
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. base1 << "/usr/local/share/applications" // 0
    2. << "/usr/share/applications" // 1
    3. << "/home/pi/.local/share/applications"; // 2
    4.  
    5. base = new QStringList();
    6. base->append(base1);
    7.  
    8. baseDir[1].setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); //| QDir::NoSymLinks);
    9. baseDir[2].setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
    10. baseDir[1] = base[1];
    11. baseDir[2] = base[2];
    12.  
    13. fileList[1] = new QStringList();
    14. fileList[2] = new QStringList();
    15.  
    16. fileList[1]->append(baseDir[1].entryList());
    17. fileList[2]->append(baseDir[2].entryList());
    18. //qDebug() << *fileList[1] << "\n";
    19. //qDebug() << *fileList[2] << "\n\n";
    20. QString file1;
    21. for (int i = 0; i < fileList[1]->count(); i++) {
    22. file1 = baseDir[1].path() + "/" + fileList[1]->at(i);
    23. QFileInfo checkFile(file1);
    24. fileType(checkFile, i);
    25. }
    26. for (int i = 0; i < fileList[2]->count(); i++) {
    27. file1 = baseDir[2].path() + "/" + fileList[2]->at(i);
    28. QFileInfo checkFile(file1);
    29. fileType(checkFile, i);
    30. }
    31.  
    32. subMenu->addItems(*fileList[1]);
    33. subMenu->addItems(*fileList[2]);
    34. subMenu->sortItems();
    35. fileTypeColour();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void menu::fileType(QFileInfo checkFile, int i)
    2. {
    3. if (checkFile.isSymLink()) fileList[1]->replaceInStrings(fileList[1]->at(i), "Symlink - " +
    4. fileList[1]->at(i) +
    5. " - Target = " +
    6. checkFile.symLinkTarget());
    7. else if (checkFile.isDir()) fileList[1]->replaceInStrings(fileList[1]->at(i), "Directory - " +
    8. fileList[1]->at(i));
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void menu::fileTypeColour()
    2. {
    3. for (int i = 0; i < subMenu->count(); i++) {
    4. if (subMenu->item(i)->text().startsWith("Symlink")) subMenu->item(i)->setForeground(*(new QBrush(Qt::darkCyan)));
    5. else if (subMenu->item(i)->text().startsWith("Directory")) subMenu->item(i)->setForeground(*(new QBrush(Qt::magenta)));
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jimbo; 7th March 2015 at 13:28.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT4/C++ - Problem with QFileInfo

    Open a terminal window and change directory to the parent directory of alsamixergui.desktop. Then run the command "file alsamixergui.desktop" and it will show you whether this is a file or directory entry. Pretty sure based on your output, it's a directory, not a file.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT4/C++ - Problem with QFileInfo

    General observations:
    - you use lots of heap allocated objects for no apparent reason (base, fileList[1], fileList[2])
    - Why manually create QFileInfo instances for each entry in a directory when you can let QDir do that for you? QDir::entryInfoList().

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    jimbo (8th March 2015)

  5. #4
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT4/C++ - Problem with QFileInfo

    Hello,

    Quote Originally Posted by jthomps View Post
    Pretty sure based on your output, it's a directory, not a file.
    Tried that and it shows as an ASCI file.
    Quote Originally Posted by anda_skoa View Post
    General observations:
    - Why manually create QFileInfo instances for each entry in a directory when you can let QDir do that for you? QDir::entryInfoList().
    I'm a relative newbee and didn't know about QDir::entryInfoList().
    I'll try that and see how I get on.

    Many thanks for both your replies.

    Regards

  6. #5
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT4/C++ - Problem with QFileInfo

    Hello anda_skoa,

    Many thanks for your pointers.
    Works a treat.

    Regards
    Qt Code:
    1. base1 << "/usr/local/share/applications" // 0
    2. << "/usr/share/applications" // 1
    3. << "/home/pi/.local/share/applications"; // 2
    4.  
    5. fileList[1] = new QStringList();
    6.  
    7. fileList[1]->append(getList(QDir(base1.at(1))));
    8. fileList[1]->append(getList(QDir(base1.at(2))));
    9.  
    10. subMenu->addItems(*fileList[1]);
    11. subMenu->sortItems();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QStringList menu::getList(QDir dir)
    2. {
    3. QStringList fileList;
    4. QFileInfoList fList = dir.entryInfoList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
    5. for (int i = 0; i < fList.size(); i++) {
    6. if (fList.at(i).isSymLink() || fList.at(i).isDir()) {
    7. if (fList.at(i).isDir()) fileList.append("Directory - " + fList.at(i).fileName());
    8. else fileList.append("Symlink - " + fList.at(i).fileName()
    9. + " - Target = "
    10. + fList.at(i).symLinkTarget());
    11. } else fileList.append(fList.at(i).fileName());
    12. }
    13. return (fileList);
    14. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT4/C++ - Problem with QFileInfo

    Quote Originally Posted by jimbo View Post
    Many thanks for your pointers.
    Works a treat.
    So what's the final verdict, was it a file or directory?

  8. #7
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT4/C++ - Problem with QFileInfo

    Hello,
    Quote Originally Posted by jthomps View Post
    So what's the final verdict, was it a file or directory?
    Its a file, as suspected.

    Regards
    Last edited by jimbo; 9th March 2015 at 08:58.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT4/C++ - Problem with QFileInfo

    Quote Originally Posted by jimbo View Post
    Qt Code:
    1. fileList[1] = new QStringList();
    To copy to clipboard, switch view to plain text mode 
    You really don't need "new" for a QStringList.
    You are just making your live more difficult with having to manage memory manually.

    Cheers,
    _

Similar Threads

  1. QFileInfo is very slow
    By jho in forum Qt Programming
    Replies: 20
    Last Post: 21st January 2015, 12:43
  2. QFileInfo::created() Problem Under POSIX!!
    By Mans in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2011, 02:34
  3. QFileInfo
    By ishkabible in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2010, 00:08
  4. Is there such a way QFileInfo
    By baray98 in forum Qt Programming
    Replies: 9
    Last Post: 21st April 2008, 03:23
  5. about QFileInfo
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2006, 10:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.