Results 1 to 4 of 4

Thread: Can folder color be changed in QTreeView when QFilesystemModel displays drives?

  1. #1
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Can folder 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.
    Qt Code:
    1. QFileSystemModel *model = new QFileSystemModel;
    2. model->setRootPath(QDir::currentPath())
    3. tree->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    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”???

  2. The following user says thank you to owais_blore for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can folder color be changed in QTreeView when QFilesystemModel displays drives?

    Quote Originally Posted by owais_blore View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following 2 users say thank you to wysota for this useful post:

    owais_blore (11th December 2012)

  5. #3
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Can folder color be changed in QTreeView when QFilesystemModel displays drives?

    This is what I did now:
    In header file:
    Qt Code:
    1. class MyQFileSystemModel : public QFileSystemModel {
    2. public:
    3. QVariant data(const QModelIndex &index, int role) const;
    4. };
    To copy to clipboard, switch view to plain text mode 

    In cpp File:
    Qt Code:
    1. QVariant MyQFileSystemModel::data( const QModelIndex& index, int role ) const {
    2.  
    3. if( role == Qt::DecorationRole ) {
    4. return QVariant(QIcon(QPixmap(":\\P2Viewer\\Foldericon.PNG")));
    5. }
    6.  
    7. return QFileSystemModel::data(index, role);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Inside the constructor:
    Qt Code:
    1. MyQFileSystemModel* model = new MyQFileSystemModel;
    2. model->setRootPath(QDir::currentPath());
    3. ui->SecoTreeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    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.
    Quote Originally Posted by wysota View Post
    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.
    Last edited by owais_blore; 11th December 2012 at 12:12.

  6. The following user says thank you to owais_blore for this useful post:


  7. #4
    Join Date
    Feb 2012
    Posts
    24
    Thanks
    4
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can folder color be changed in QTreeView when QFilesystemModel displays drives?

    Made a class which implements QIconProvider and it worked
    For future reference, here is the code:
    Qt Code:
    1. MyIconProvider::MyIconProvider()
    2. {
    3. m_Folder = QIcon(":\\P2Viewer\\Foldericon.PNG");
    4. m_hd = QIcon(":\\P2Viewer\\DriveIcon.PNG");
    5. m_default = QIcon(":\\P2Viewer\\Details.PNG");
    6. }
    7.  
    8. MyIconProvider::~MyIconProvider()
    9. {
    10. }
    11.  
    12. QIcon MyIconProvider::icon( const QFileInfo & info ) const
    13. {
    14. if(info.isRoot())
    15. {
    16. return m_hd;
    17. }
    18. else if(info.isDir())
    19. {
    20. return m_Folder;
    21. }
    22.  
    23. return m_default;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Header File:
    Qt Code:
    1. class MyIconProvider : public QFileIconProvider
    2. {
    3. public:
    4. MyIconProvider();
    5. ~MyIconProvider();
    6.  
    7. QIcon icon( const QFileInfo & info ) const;
    8.  
    9. private:
    10. QIcon m_Folder;
    11. QIcon m_hd;
    12. QIcon m_default;
    13. };
    To copy to clipboard, switch view to plain text mode 

    In MainWIndow:
    Qt Code:
    1. IconProv = new MyIconProvider();
    2. pSystemPrimaryModel = new QFileSystemModel();
    3. pSystemPrimaryModel->setRootPath(QDir::currentPath());
    4. pSystemPrimaryModel->setIconProvider(IconProv);
    5. pSystemPrimaryModel->setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );
    6. ui->PrimTreeView->setModel(pSystemPrimaryModel);
    To copy to clipboard, switch view to plain text mode 

    I hope this helps someone

  8. The following user says thank you to owais_blore for this useful post:


Similar Threads

  1. Replies: 5
    Last Post: 12th December 2012, 18:45
  2. Display System Drives in Specific Order in QTreeView
    By owais_blore in forum Qt Programming
    Replies: 6
    Last Post: 5th December 2012, 08:07
  3. QFileSystemModel with network drives not connected
    By GothmoG in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 01:18
  4. Replies: 10
    Last Post: 25th November 2010, 11:33
  5. Splitter Color changed when referesh?
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2007, 08:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.