User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

QT4/C++ - Problem with QFileInfo

Thu Mar 05, 2015 11:49 am

Hello,

I'm trying to understand QFileInfo.
The problem I've got is that an entry 'alsamixergui' 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
list.jpg
list.jpg (24.66 KiB) Viewed 1307 times
properties.jpg
properties.jpg (57.82 KiB) Viewed 1307 times

Code: Select all

private:
    QListWidget *subMenu;
    QStringList *fileList[19], *base;
    QDir         baseDir[19];

Code: Select all

    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();

Code: Select all

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: Select all

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)));
    }
}

User avatar
topguy
Posts: 6060
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: QT4/C++ - Problem with QFileInfo

Thu Mar 05, 2015 5:17 pm

Some other relevant information would also be the result from "ls -al" in that directory.

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: QT4/C++ - Problem with QFileInfo

Thu Mar 05, 2015 9:24 pm

Hello,
topguy wrote:Some other relevant information would also be the result from "ls -al" in that directory.

Code: Select all

pi@mypi /usr/share/applications $ ls -al
total 576
drwxr-xr-x   3 root root  4096 Mar  1 10:08 .
drwxr-xr-x 289 root root 12288 Feb 28 16:19 ..
-rw-r--r--   1 root root   199 May  8  2012 alsamixergui.desktop
Thanks for the reply.
I'm afraid this doesn't mean to much to me, if I understand correctly its r/w by root and read only by anyone else.
Perhaps you can interpret the results.

Regards

User avatar
topguy
Posts: 6060
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: QT4/C++ - Problem with QFileInfo

Fri Mar 06, 2015 11:41 am

I thought it might be more files in that directory, but I saw later that you fetch files from more than one directory.

My only theory ( without fine-reading your code ) is that you also pick up "." and ".." when listing files and those will check out as directories.

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: QT4/C++ - Problem with QFileInfo

Fri Mar 06, 2015 12:06 pm

Hello,
topguy wrote:I thought it might be more files in that directory
There are more files listed, I just showed the file of interest.
It shows directories, see the entry in the attachment for 'list.jpeg' for kde4.
The problem is that it is showing 'alsemixergui.desktop' as a directory when I think it should be a file.

Regards

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: QT4/C++ - Problem with QFileInfo

Sat Mar 07, 2015 8:34 pm

Hello,

I've just tested the same source code on Debian Wheezy on my PC.
Works as it is supposed to.
Anybody have any ideas.

Regards
Attachments
Image1.jpg
Image1.jpg (28.74 KiB) Viewed 1138 times

User avatar
topguy
Posts: 6060
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: QT4/C++ - Problem with QFileInfo

Tue Mar 10, 2015 11:41 am

I would have added some debug printout to "menu::fileType()" long time ago, thats where the error most likely will appear.

And if you look closer you will see that you always manipulate fileList[1] inside that function, but you are also using that function from the loop where you loop through the files in fileList[2], which is a completely different list of files.

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: QT4/C++ - Problem with QFileInfo

Tue Mar 10, 2015 1:41 pm

Hello topguy,

Thanks for your reply.
I realised there was something basically wrong with my method and got it working with some help from another forum.
http://www.qtcentre.org/threads/61821-Q ... -QFileInfo

Regards

Code: Select all

    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))));
    fileList[1]->sort();

    subMenu->addItems(*fileList[1]);
    subMenu->sortItems();

Code: Select all

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(fList.at(i).fileName() + " - Directory");
            else fileList.append(fList.at(i).fileName() + " - Symlink - Target = "
                                 + fList.at(i).symLinkTarget());
        } else fileList.append(fList.at(i).fileName());
    }
    return (fileList);
}

Return to “C/C++”