PDA

View Full Version : QT4/C++ - Problem with QFileInfo



jimbo
7th March 2015, 14:21
Hello,

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

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/dzrqnqu40ep7z49/AAD7-rdxqN21wgerA0RpkI48a?dl=0

private:
QListWidget *subMenu;
QStringList *fileList[19], *base;
QDir baseDir[19];
QStringList base1;
base1 << "/usr/local/share/applications" // 0
<< "/usr/share/applications" // 1
<< "/home/pi/.local/share/applications"; // 2

base = new QStringList();
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] = new QStringList();
fileList[2] = new QStringList();

fileList[1]->append(baseDir[1].entryList());
fileList[2]->append(baseDir[2].entryList());
//qDebug() << *fileList[1] << "\n";
//qDebug() << *fileList[2] << "\n\n";
QString file1;
for (int i = 0; i < fileList[1]->count(); i++) {
file1 = baseDir[1].path() + "/" + fileList[1]->at(i);
QFileInfo checkFile(file1);
fileType(checkFile, i);
}
for (int i = 0; i < fileList[2]->count(); i++) {
file1 = baseDir[2].path() + "/" + fileList[2]->at(i);
QFileInfo checkFile(file1);
fileType(checkFile, i);
}

subMenu->addItems(*fileList[1]);
subMenu->addItems(*fileList[2]);
subMenu->sortItems();
fileTypeColour();
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));
}
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)));
}
}

jefftee
7th March 2015, 21:49
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.

anda_skoa
8th March 2015, 11:15
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,
_

jimbo
8th March 2015, 11:48
Hello,


Pretty sure based on your output, it's a directory, not a file.
Tried that and it shows as an ASCI file.

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

jimbo
8th March 2015, 21:14
Hello anda_skoa,

Many thanks for your pointers.
Works a treat.

Regards

QStringList base1;
base1 << "/usr/local/share/applications" // 0
<< "/usr/share/applications" // 1
<< "/home/pi/.local/share/applications"; // 2

fileList[1] = new QStringList();

fileList[1]->append(getList(QDir(base1.at(1))));
fileList[1]->append(getList(QDir(base1.at(2))));

subMenu->addItems(*fileList[1]);
subMenu->sortItems();
QStringList menu::getList(QDir dir)
{
QStringList fileList;
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);
}

jefftee
8th March 2015, 21:40
Many thanks for your pointers.
Works a treat.

So what's the final verdict, was it a file or directory? :)

jimbo
9th March 2015, 09:51
Hello,

So what's the final verdict, was it a file or directory? :)
Its a file, as suspected.

Regards

anda_skoa
9th March 2015, 09:58
fileList[1] = new QStringList();

You really don't need "new" for a QStringList.
You are just making your live more difficult with having to manage memory manually.

Cheers,
_