Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Can FolderIcon color be changed in QTreeView when QFilesystemModel displays drives?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 8.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stoned Jesus
    wrote on last edited by
    #1

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

    --
    Thanks & Regards,
    Stoned Jesus

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hpollak
      wrote on last edited by
      #2

      I think you can do this by Stylesheets see:

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Stoned Jesus
        wrote on last edited by
        #3

        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]

        --
        Thanks & Regards,
        Stoned Jesus

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Stoned Jesus
          wrote on last edited by
          #4

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

          --
          Thanks & Regards,
          Stoned Jesus

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hpollak
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hpollak
              wrote on last edited by
              #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
              ...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Stoned Jesus
                wrote on last edited by
                #7

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

                --
                Thanks & Regards,
                Stoned Jesus

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  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.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Stoned Jesus
                    wrote on last edited by
                    #9

                    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]

                    --
                    Thanks & Regards,
                    Stoned Jesus

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hpollak
                      wrote on last edited by
                      #10

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

                      @

                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        Stoned Jesus
                        wrote on last edited by
                        #11

                        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]

                        --
                        Thanks & Regards,
                        Stoned Jesus

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          hpollak
                          wrote on last edited by
                          #12

                          sorry:

                          iconProv is the instance of the myIconProvider - Class

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Stoned Jesus
                            wrote on last edited by
                            #13

                            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]

                            --
                            Thanks & Regards,
                            Stoned Jesus

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Stoned Jesus
                              wrote on last edited by
                              #14

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

                              --
                              Thanks & Regards,
                              Stoned Jesus

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved