Blessed Geek
Blessed Geek

Reputation: 21614

Qt: Change icon in a QFileInfo object

How do I set/change the file icon in a QFileInfo object? If you look at my code, Qlist<QFileInfo> lists the icon of all my folders in my home directory as gnome-fs-directory. Which means, QFileInfo lists even my desktop folder's icon as plain gnome-fs-directory.

But I want Desktop to have QFileIconProvider::Desktop as icon.

Is QFileInfo the appropriate class to use to find out the icon that QFileSystemModel would use?

Why did my QDir not pass QFileSystemModel a QFileInfo list with the appropriate icon role for Desktop?

Code to find out the file icon of each folder in the home folder:

void MainWindow::fileIconInfo(QFileSystemModel *model)
{
    QFileIconProvider *iconprov = model->iconProvider();

    QFileInfoList fileInfoList = QDir::home().entryInfoList();
    QFileInfoList::Iterator i;

    foreach (QFileInfo fi, fileInfoList){

        if (fi.fileName() == QString("Desktop"))
            /*change the icon to QFileIconProvider::Desktop*/;

        //the following line indicates all my icons are gnome-fs-directory!!*/
        std::cout << iconprov->icon(fi).name().toStdString() << std::endl;
    }
}

This is my main window:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    fileSystemTree(ui->listView);
    fileSystemTree(ui->treeView);
}

Setting the model for the view:

void MainWindow::fileSystemTree(QAbstractItemView *view) {
    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::homePath());

    view->setModel(model);
    view->setRootIndex(model->index(QDir::homePath()));

    fileIconInfo(model);
}

Upvotes: 1

Views: 1456

Answers (1)

j_kubik
j_kubik

Reputation: 6181

I think what you are describing is caused by the fact that QFileIconProvider is detecting that you use Gnome, and the is using Gtk style - no matter what. Could you try to start some other desktop environment and see if problem remains? If it does then I am right and only thing you can do is to subclass QFileSystemModel and change QIcon returned from data method - but this is quite crude and non-flexible solution.

Upvotes: 1

Related Questions