[SOLVED] Can FolderIcon color be changed in QTreeView when QFilesystemModel displays drives?



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



  • I think you can do this by Stylesheets see:

    "Stylesheet Example":http://doc.qt.digia.com/qt/stylesheet-examples.html



  • I have been working with stylesheet for sometime but I dont think we can change the folder color using stylesheet. Any other approach???
    [quote author="hpollak" date="1355219458"]I think you can do this by Stylesheets see:

    "Stylesheet Example":http://doc.qt.digia.com/qt/stylesheet-examples.html[/quote]



  • I think I would need to reimplement its data() method to return an icon of your choice:
    @QVariant MyQFileSystemModel::data( const QModelIndex& index, int role ) const {

    if( role == Qt::DecorationRole ) { 
        return QPixmap("icon.png");
    }
    
    return QFileSystemModel::data(index, role);
    

    }@

    How do I go ahead with it? Damn!!!



  • QFileSystemModel has a method called "setIconProvider()". generate your own IconProvider and give it to the model, so you should be able to set the icons for those types:

    QFileIconProvider::Computer 0
    QFileIconProvider::Desktop 1
    QFileIconProvider::Trashcan 2
    QFileIconProvider::Network 3
    QFileIconProvider::Drive 4
    QFileIconProvider::Folder 5
    QFileIconProvider::File 6



  • But i think there should be a way to do this with stylesheets.

    Cause in the in the Documentation "Stylesheet-refernce":http://doc.qt.digia.com/qt/stylesheet-reference.html#branch-sub:

    List of Icons
    Icons used in Qt can be customized using the following properties. Each of the properties listed in this section have the type Icon.

    ...
    dvd-icon QStyle::SP_DriveDVDIcon
    file-icon QStyle::SP_FileIcon
    ...



  • This is what I did:
    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 QPixmap(":\P2Viewer\Detail.PNG");
    }
     
    return QFileSystemModel::data(index, role);
    }@
    

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

    when I run the app, it doesn't display the folders anymore inside the TreeView but the image which I have set in QPixMap doesnt appear.
    Where am i making a mistake???



  • Are you sure, the resource is included and the path is correct?
    Additionally, you add a decoration role to each column, is that what you want?
    I would use forward slashes for the path.



  • Hey Gerolf here is how I changed the .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);
    

    }@

    This displays the image as expected. But the issue here is the decoration role. PLus along with folders the images are getting displayed for the drives too. Ideally only the folders/subfolders of C: or D: drive must display this image and I want to keep a separate image for drives.

    What needs to be done to make sure only for folders a separate image appears and for Drive a separate one?

    [quote author="Gerolf" date="1355230817"]Are you sure, the resource is included and the path is correct?
    Additionally, you add a decoration role to each column, is that what you want?
    I would use forward slashes for the path.[/quote]



  • Another way (Quick & Dirty implemented):

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

    //enum IconType { Computer, Desktop, Trashcan, Network, Drive, Folder, File };
    
    QIcon icon( IconType type ) const;
    QIcon icon( const QFileInfo & info ) const;
    QString type( const QFileInfo & info ) const;
    

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

    @MyIconProvider::MyIconProvider()
    {
    m_Folder = QIcon(":/images/folder.png");
    m_hd = QIcon(":/images/hd.png");
    m_default = QIcon(":/images/icon.png");
    }

    MyIconProvider::~MyIconProvider()
    {
    }

    QIcon MyIconProvider::icon( IconType type ) const
    {
    if(type==Folder) return m_Folder;

    if(type==Drive) return m_hd;
    
    
    return m_default;
    

    }

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

    QString MyIconProvider::type ( const QFileInfo & info ) const
    {
    if(info.isDir()) return QString("Verzeichniss");
    if(info.isRoot()) return QString("Drive");
    return QString("File");
    }
    @

    and then:

    @ model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());

    model->setIconProvider(iconProv);
    
    treeView->setModel(model);
    

    @



  • Thanks mate...... But what's iconProv???? U have used it but it ain't declared anywhere
    [quote author="hpollak" date="1355236467"]Another way (Quick & Dirty implemented):

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

    //enum IconType { Computer, Desktop, Trashcan, Network, Drive, Folder, File };
    
    QIcon icon( IconType type ) const;
    QIcon icon( const QFileInfo & info ) const;
    QString type( const QFileInfo & info ) const;
    

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

    @MyIconProvider::MyIconProvider()
    {
    m_Folder = QIcon(":/images/folder.png");
    m_hd = QIcon(":/images/hd.png");
    m_default = QIcon(":/images/icon.png");
    }

    MyIconProvider::~MyIconProvider()
    {
    }

    QIcon MyIconProvider::icon( IconType type ) const
    {
    if(type==Folder) return m_Folder;

    if(type==Drive) return m_hd;
    
    
    return m_default;
    

    }

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

    QString MyIconProvider::type ( const QFileInfo & info ) const
    {
    if(info.isDir()) return QString("Verzeichniss");
    if(info.isRoot()) return QString("Drive");
    return QString("File");
    }
    @

    and then:

    @ model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());

    model->setIconProvider(iconProv);
    
    treeView->setModel(model);
    

    @[/quote]



  • sorry:

    iconProv is the instance of the myIconProvider - Class



  • Well tried the same way, had few errors and cleared them. Finally I find the same output displaying everything with respect to m_Folder icon which my prev code was displaying..... :(

    [quote author="hpollak" date="1355238026"]sorry:

    iconProv is the instance of the myIconProvider - Class

    [/quote]



  • Done...... I had to make few changes to the code and it works now :)
    Thanks hpollak :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.