Domanda

Well I have been working on a Qt app where I need to display Filesystem using QFilesystemModel.I have been able to display it as expected.

Code:

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath())
tree->setModel(model);

This displays all drives inside QTreeView. But we all know by default, the color of the folders present inside each drive is yellow.

This is what i wanna change. Is there a way in Qt, where one can change the color of folder to "Blue"???

È stato utile?

Soluzione

You can customize your QTreeView via a qt delegate. But if it is just for an icon, I think you can reimplement:

class MyQFileSystemModel : public QFileSystemModel {
public:
    QVariant data( const QModelIndex& index, int role ) const {

        if( role == Qt::DecorationRole ) { 
            return QVariant(QIcon(QPixmap("icon.png")));
        }

        return QFileSystemModel::data(index, role);
    }
};

To learn about delegate, I suggest you study the examples: Spin box delegate

EDIT: you have to reimplement the method data from a QFileSystemModel, you must inherit from QFileSystemModel.

Then you do as before:

MyQFileSystemModel* model = new MyQFileSystemModel;
model->setRootPath(QDir::currentPath())
tree->setModel(model);

Altri suggerimenti

I would assume that the workaround is to use your own fileIcon.

Qt uses whatever the default icon is for the current platform. I'm guessing you're on Windows.

This question provides some clues on how to implement.

(I just started learning Qt myself, BTW!)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top