PDA

View Full Version : Can folder color be changed in QTreeView when QFilesystemModel displays drives?



owais_blore
11th December 2012, 09:26
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.

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

wysota
11th December 2012, 10:04
This displays all drives inside QTreeView. But we all know by default, the color of the folders present inside each drive is yellow.
No, we don't know that. If you mean the folder icon then on my system it is not 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 subclass the model and reimplement its data() method to return an icon of your choice or you can provide a custom delegate that will render each item in a way you like. And no, there is no QMakeMyFolderIconBlueDelegate class, you have to write it yourself.

owais_blore
11th December 2012, 11:37
This is what I did now:
In header file:

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

In cpp File:

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

if( role == Qt::DecorationRole ) {
return QVariant(QIcon(QPixmap(":\\P2Viewer\\Foldericon.PNG")));
}

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

Inside the constructor:

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

This changes the foldericon but also changes the icon of C: or D: drive. Basically I want to use 2 icons... One for folders within drives and other for the drive.

No, we don't know that. If you mean the folder icon then on my system it is not yellow.

You can subclass the model and reimplement its data() method to return an icon of your choice or you can provide a custom delegate that will render each item in a way you like. And no, there is no QMakeMyFolderIconBlueDelegate class, you have to write it yourself.

owais_blore
11th December 2012, 16:32
Made a class which implements QIconProvider and it worked :)
For future reference, here is the code:

MyIconProvider::MyIconProvider()
{
m_Folder = QIcon(":\\P2Viewer\\Foldericon.PNG");
m_hd = QIcon(":\\P2Viewer\\DriveIcon.PNG");
m_default = QIcon(":\\P2Viewer\\Details.PNG");
}

MyIconProvider::~MyIconProvider()
{
}

QIcon MyIconProvider::icon( const QFileInfo & info ) const
{
if(info.isRoot())
{
return m_hd;
}
else if(info.isDir())
{
return m_Folder;
}

return m_default;
}

Header File:

class MyIconProvider : public QFileIconProvider
{
public:
MyIconProvider();
~MyIconProvider();

QIcon icon( const QFileInfo & info ) const;

private:
QIcon m_Folder;
QIcon m_hd;
QIcon m_default;
};

In MainWIndow:

IconProv = new MyIconProvider();
pSystemPrimaryModel = new QFileSystemModel();
pSystemPrimaryModel->setRootPath(QDir::currentPath());
pSystemPrimaryModel->setIconProvider(IconProv);
pSystemPrimaryModel->setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );
ui->PrimTreeView->setModel(pSystemPrimaryModel);

I hope this helps someone :)