문제

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"???

도움이 되었습니까?

해결책

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

다른 팁

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!)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top