1 Attachment(s)
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
Attachment 10999
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
Code:
base1 << "/usr/local/share/applications" // 0
<< "/usr/share/applications" // 1
<< "/home/pi/.local/share/applications"; // 2
base->append(base1);
baseDir
[1].
setFilter(QDir::AllDirs |
QDir::Files |
QDir::NoDotAndDotDot);
//| QDir::NoSymLinks); baseDir
[2].
setFilter(QDir::AllDirs |
QDir::Files |
QDir::NoDotAndDotDot);
baseDir[1] = base[1];
baseDir[2] = base[2];
fileList[1]->append(baseDir[1].entryList());
fileList[2]->append(baseDir[2].entryList());
//qDebug() << *fileList[1] << "\n";
//qDebug() << *fileList[2] << "\n\n";
for (int i = 0; i < fileList[1]->count(); i++) {
file1 = baseDir[1].path() + "/" + fileList[1]->at(i);
fileType(checkFile, i);
}
for (int i = 0; i < fileList[2]->count(); i++) {
file1 = baseDir[2].path() + "/" + fileList[2]->at(i);
fileType(checkFile, i);
}
subMenu->addItems(*fileList[1]);
subMenu->addItems(*fileList[2]);
subMenu->sortItems();
fileTypeColour();
Code:
void menu
::fileType(QFileInfo checkFile,
int i
) {
if (checkFile.isSymLink()) fileList[1]->replaceInStrings(fileList[1]->at(i), "Symlink - " +
fileList[1]->at(i) +
" - Target = " +
checkFile.symLinkTarget());
else if (checkFile.isDir()) fileList[1]->replaceInStrings(fileList[1]->at(i), "Directory - " +
fileList[1]->at(i));
}
Code:
void menu::fileTypeColour()
{
for (int i = 0; i < subMenu->count(); i++) {
if (subMenu
->item
(i
)->text
().
startsWith("Symlink")) subMenu
->item
(i
)->setForeground
(*(new QBrush(Qt
::darkCyan)));
else if (subMenu
->item
(i
)->text
().
startsWith("Directory")) subMenu
->item
(i
)->setForeground
(*(new QBrush(Qt
::magenta)));
}
}
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.
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,
_
Re: QT4/C++ - Problem with QFileInfo
Hello,
Quote:
Originally Posted by
jthomps
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
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
Re: QT4/C++ - Problem with QFileInfo
Hello anda_skoa,
Many thanks for your pointers.
Works a treat.
Regards
Code:
base1 << "/usr/local/share/applications" // 0
<< "/usr/share/applications" // 1
<< "/home/pi/.local/share/applications"; // 2
fileList
[1]->append
(getList
(QDir(base1.
at(1))));
fileList
[1]->append
(getList
(QDir(base1.
at(2))));
subMenu->addItems(*fileList[1]);
subMenu->sortItems();
Code:
{
QFileInfoList fList
= dir.
entryInfoList(QDir::AllDirs |
QDir::Files |
QDir::NoDotAndDotDot);
for (int i = 0; i < fList.size(); i++) {
if (fList.at(i).isSymLink() || fList.at(i).isDir()) {
if (fList.at(i).isDir()) fileList.append("Directory - " + fList.at(i).fileName());
else fileList.append("Symlink - " + fList.at(i).fileName()
+ " - Target = "
+ fList.at(i).symLinkTarget());
} else fileList.append(fList.at(i).fileName());
}
return (fileList);
}
Re: QT4/C++ - Problem with QFileInfo
Quote:
Originally Posted by
jimbo
Many thanks for your pointers.
Works a treat.
So what's the final verdict, was it a file or directory? :)
Re: QT4/C++ - Problem with QFileInfo
Hello,
Quote:
Originally Posted by
jthomps
So what's the final verdict, was it a file or directory? :)
Its a file, as suspected.
Regards
Re: QT4/C++ - Problem with QFileInfo
Quote:
Originally Posted by
jimbo
You really don't need "new" for a QStringList.
You are just making your live more difficult with having to manage memory manually.
Cheers,
_