Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for MCUs
  4. How can I change the Qtreeview directory(folder) icon for different folders?
QtWS25 Last Chance

How can I change the Qtreeview directory(folder) icon for different folders?

Scheduled Pinned Locked Moved Unsolved Qt for MCUs
15 Posts 2 Posters 1.9k Views
  • 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
    sandip.nandwana
    wrote on 16 Sept 2022, 11:29 last edited by
    #1

    Dir.png

    J 1 Reply Last reply 16 Sept 2022, 11:34
    0
    • S sandip.nandwana
      16 Sept 2022, 11:29

      Dir.png

      J Offline
      J Offline
      JonB
      wrote on 16 Sept 2022, 11:34 last edited by JonB
      #2

      @sandip-nandwana
      Hello and welcome.

      You mean you want e.g. one icon against the Case... folder and a different one against the Log... folder? Oh, I see, ScreenCapture is also a folder, it has a different icon, and this screenshot is from another application you want your Qt app to look like, right?

      What model are you using for your QTreeView?

      S 1 Reply Last reply 16 Sept 2022, 11:50
      0
      • J JonB
        16 Sept 2022, 11:34

        @sandip-nandwana
        Hello and welcome.

        You mean you want e.g. one icon against the Case... folder and a different one against the Log... folder? Oh, I see, ScreenCapture is also a folder, it has a different icon, and this screenshot is from another application you want your Qt app to look like, right?

        What model are you using for your QTreeView?

        S Offline
        S Offline
        sandip.nandwana
        wrote on 16 Sept 2022, 11:50 last edited by
        #3

        @JonB Yes, I want to change the icon only for the case and log folder but not for the children folder, and I am using "QFileSystemModel" model.

        J 1 Reply Last reply 16 Sept 2022, 12:02
        0
        • S sandip.nandwana
          16 Sept 2022, 11:50

          @JonB Yes, I want to change the icon only for the case and log folder but not for the children folder, and I am using "QFileSystemModel" model.

          J Offline
          J Offline
          JonB
          wrote on 16 Sept 2022, 12:02 last edited by JonB
          #4

          @sandip-nandwana
          Then subclass QFileSystemModel, override data() virtual method and do what you want for Qt::DecorationRole or QFileSystemModel::FileIconRole role, taking index of individual item into account. This is illustrated in https://stackoverflow.com/questions/27587035/qfilesystemmodel-custom-icons.

          S 2 Replies Last reply 16 Sept 2022, 12:57
          1
          • J JonB
            16 Sept 2022, 12:02

            @sandip-nandwana
            Then subclass QFileSystemModel, override data() virtual method and do what you want for Qt::DecorationRole or QFileSystemModel::FileIconRole role, taking index of individual item into account. This is illustrated in https://stackoverflow.com/questions/27587035/qfilesystemmodel-custom-icons.

            S Offline
            S Offline
            sandip.nandwana
            wrote on 16 Sept 2022, 12:57 last edited by
            #5

            @JonB Thanks, I will try this.

            1 Reply Last reply
            0
            • J JonB
              16 Sept 2022, 12:02

              @sandip-nandwana
              Then subclass QFileSystemModel, override data() virtual method and do what you want for Qt::DecorationRole or QFileSystemModel::FileIconRole role, taking index of individual item into account. This is illustrated in https://stackoverflow.com/questions/27587035/qfilesystemmodel-custom-icons.

              S Offline
              S Offline
              sandip.nandwana
              wrote on 19 Sept 2022, 09:19 last edited by
              #6

              @JonB I tried that, but all the icons changed.Dir.png

              J 1 Reply Last reply 19 Sept 2022, 09:40
              0
              • S sandip.nandwana
                19 Sept 2022, 09:19

                @JonB I tried that, but all the icons changed.Dir.png

                J Offline
                J Offline
                JonB
                wrote on 19 Sept 2022, 09:40 last edited by JonB
                #7

                @sandip-nandwana
                So what did you do about

                taking index of individual item into account.

                ? If you do not use the index to look at the filename and decide from that which icon you want --- assuming you want to distinguish by filename, you don't say what your actual criteria for different icons are --- it won't work by magic, how do you expect it to know? So show the code you have to decide/produce which icon for which item?

                S 1 Reply Last reply 20 Sept 2022, 05:33
                1
                • J JonB
                  19 Sept 2022, 09:40

                  @sandip-nandwana
                  So what did you do about

                  taking index of individual item into account.

                  ? If you do not use the index to look at the filename and decide from that which icon you want --- assuming you want to distinguish by filename, you don't say what your actual criteria for different icons are --- it won't work by magic, how do you expect it to know? So show the code you have to decide/produce which icon for which item?

                  S Offline
                  S Offline
                  sandip.nandwana
                  wrote on 20 Sept 2022, 05:33 last edited by sandip.nandwana
                  #8

                  @JonB everything works fine but the subfolder icon changes slowly (first it shows the file icon after 500ms it shows my new icon), below is the code.

                  QVariant MyFileSystemModel::data(const QModelIndex& index, int role) const
                  {
                  QFileInfo info = MyFileSystemModel::fileInfo(index);
                  QString dirName = info.absoluteFilePath().split("/").last();
                  QStringList tem = info.absolutePath().split( "/" );

                  if( role == Qt::DecorationRole )
                  {
                      if( ( dirName.contains("Case") == true )  && ( index.column() == 0 ) )
                      {
                          return QPixmap( ":/images/YellowFolderIcon.png" );
                      }
                      else if( ( dirName.contains("Log") == true ) && ( index.column() == 0 ) )
                      {
                          return QPixmap( ":/images/BlueFolderIcon.png" );
                      }
                      else if( ( dirName.contains("Screen") == true ) && ( index.column() == 0 ) )
                      {
                          if( ( tem.length() >= 4 ) && QString(tem.at(4)).contains("Case") )
                          {
                              return QPixmap( ":/images/YellowFolderIcon.png" );
                          }
                          else
                          {
                              return QPixmap( ":/images/BlueFolderIcon.png" );
                          }
                      }
                  }
                  
                  return QFileSystemModel::data(index, role);
                  

                  }

                  and the filter is not working when I changed the "setNameFilters" input StringList.

                  J 1 Reply Last reply 20 Sept 2022, 07:35
                  0
                  • S sandip.nandwana
                    20 Sept 2022, 05:33

                    @JonB everything works fine but the subfolder icon changes slowly (first it shows the file icon after 500ms it shows my new icon), below is the code.

                    QVariant MyFileSystemModel::data(const QModelIndex& index, int role) const
                    {
                    QFileInfo info = MyFileSystemModel::fileInfo(index);
                    QString dirName = info.absoluteFilePath().split("/").last();
                    QStringList tem = info.absolutePath().split( "/" );

                    if( role == Qt::DecorationRole )
                    {
                        if( ( dirName.contains("Case") == true )  && ( index.column() == 0 ) )
                        {
                            return QPixmap( ":/images/YellowFolderIcon.png" );
                        }
                        else if( ( dirName.contains("Log") == true ) && ( index.column() == 0 ) )
                        {
                            return QPixmap( ":/images/BlueFolderIcon.png" );
                        }
                        else if( ( dirName.contains("Screen") == true ) && ( index.column() == 0 ) )
                        {
                            if( ( tem.length() >= 4 ) && QString(tem.at(4)).contains("Case") )
                            {
                                return QPixmap( ":/images/YellowFolderIcon.png" );
                            }
                            else
                            {
                                return QPixmap( ":/images/BlueFolderIcon.png" );
                            }
                        }
                    }
                    
                    return QFileSystemModel::data(index, role);
                    

                    }

                    and the filter is not working when I changed the "setNameFilters" input StringList.

                    J Offline
                    J Offline
                    JonB
                    wrote on 20 Sept 2022, 07:35 last edited by JonB
                    #9

                    @sandip-nandwana
                    OK, at least this code approach looks right.

                    For the "slowness": your code reads a .png file from your resources to create a QPixmap for it each time that is wanted for an individual folder's icon. I imagine that might be "slow". Also (I think) a separate in-memory QPixmap copy is kept for each folder it applies to, they do not share one created pixmap for each re-use of the same icon. I imagine that it is eating memory.

                    Store a single instance of each unique QPixmap as member variables in your class. Create/read them in the constructor, or when first wanted, as you please. Re-use those variables in your return statements. Does that increase speed?

                    and the filter is not working when I changed the "setNameFilters" input StringList.

                    Not sure what you mean. This is not to do with the icon? I would first test that setNameFilters() works as you expect if you set it to something initially. Then maybe you are saying if you change it once the QFileSystemModel has been populated it does not update to reflect the change? Maybe just try calling setRootPath() after setNameFilters() to see whether that causes it to refresh itself? Or, what are you expecting to see when a filename does not pass a name filter? Have you looked at setNameFilterDisables(bool enable)?

                    S 2 Replies Last reply 20 Sept 2022, 07:56
                    0
                    • J JonB
                      20 Sept 2022, 07:35

                      @sandip-nandwana
                      OK, at least this code approach looks right.

                      For the "slowness": your code reads a .png file from your resources to create a QPixmap for it each time that is wanted for an individual folder's icon. I imagine that might be "slow". Also (I think) a separate in-memory QPixmap copy is kept for each folder it applies to, they do not share one created pixmap for each re-use of the same icon. I imagine that it is eating memory.

                      Store a single instance of each unique QPixmap as member variables in your class. Create/read them in the constructor, or when first wanted, as you please. Re-use those variables in your return statements. Does that increase speed?

                      and the filter is not working when I changed the "setNameFilters" input StringList.

                      Not sure what you mean. This is not to do with the icon? I would first test that setNameFilters() works as you expect if you set it to something initially. Then maybe you are saying if you change it once the QFileSystemModel has been populated it does not update to reflect the change? Maybe just try calling setRootPath() after setNameFilters() to see whether that causes it to refresh itself? Or, what are you expecting to see when a filename does not pass a name filter? Have you looked at setNameFilterDisables(bool enable)?

                      S Offline
                      S Offline
                      sandip.nandwana
                      wrote on 20 Sept 2022, 07:56 last edited by
                      #10

                      @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                      Store a single instance of each unique QPixmap as member variables in your class. Create/read them in the constructor, or when first wanted, as you please. Re-use those variables in your return statements. Does that increase speed?

                      I tried before, but it had no impact.

                      J 1 Reply Last reply 20 Sept 2022, 08:09
                      0
                      • S sandip.nandwana
                        20 Sept 2022, 07:56

                        @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                        Store a single instance of each unique QPixmap as member variables in your class. Create/read them in the constructor, or when first wanted, as you please. Re-use those variables in your return statements. Does that increase speed?

                        I tried before, but it had no impact.

                        J Offline
                        J Offline
                        JonB
                        wrote on 20 Sept 2022, 08:09 last edited by
                        #11

                        @sandip-nandwana
                        Then I have no idea why anything would take 500ms, does not sound right....

                        1 Reply Last reply
                        0
                        • J JonB
                          20 Sept 2022, 07:35

                          @sandip-nandwana
                          OK, at least this code approach looks right.

                          For the "slowness": your code reads a .png file from your resources to create a QPixmap for it each time that is wanted for an individual folder's icon. I imagine that might be "slow". Also (I think) a separate in-memory QPixmap copy is kept for each folder it applies to, they do not share one created pixmap for each re-use of the same icon. I imagine that it is eating memory.

                          Store a single instance of each unique QPixmap as member variables in your class. Create/read them in the constructor, or when first wanted, as you please. Re-use those variables in your return statements. Does that increase speed?

                          and the filter is not working when I changed the "setNameFilters" input StringList.

                          Not sure what you mean. This is not to do with the icon? I would first test that setNameFilters() works as you expect if you set it to something initially. Then maybe you are saying if you change it once the QFileSystemModel has been populated it does not update to reflect the change? Maybe just try calling setRootPath() after setNameFilters() to see whether that causes it to refresh itself? Or, what are you expecting to see when a filename does not pass a name filter? Have you looked at setNameFilterDisables(bool enable)?

                          S Offline
                          S Offline
                          sandip.nandwana
                          wrote on 20 Sept 2022, 08:11 last edited by sandip.nandwana
                          #12

                          @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                          Not sure what you mean. This is not to do with the icon? I would first test that setNameFilters() works as you expect if you set it to something initially. Then maybe you are saying if you change it once the QFileSystemModel has been populated it does not update to reflect the change? Maybe just try calling setRootPath() after setNameFilters() to see whether that causes it to refresh itself? Or, what are you expecting to see when a filename does not pass a name filter? Have you looked at setNameFilterDisables(bool enable)?

                          I have set the filter to include both the "Case" and "Log" directories, and then I'll set the filter to only include the "Case" directory, but it can't filter that it also shows the "Log" directory.

                          "Shows" how? What is your setNameFilterDisables() setting? -> is false

                          J 1 Reply Last reply 20 Sept 2022, 08:12
                          0
                          • S sandip.nandwana
                            20 Sept 2022, 08:11

                            @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                            Not sure what you mean. This is not to do with the icon? I would first test that setNameFilters() works as you expect if you set it to something initially. Then maybe you are saying if you change it once the QFileSystemModel has been populated it does not update to reflect the change? Maybe just try calling setRootPath() after setNameFilters() to see whether that causes it to refresh itself? Or, what are you expecting to see when a filename does not pass a name filter? Have you looked at setNameFilterDisables(bool enable)?

                            I have set the filter to include both the "Case" and "Log" directories, and then I'll set the filter to only include the "Case" directory, but it can't filter that it also shows the "Log" directory.

                            "Shows" how? What is your setNameFilterDisables() setting? -> is false

                            J Offline
                            J Offline
                            JonB
                            wrote on 20 Sept 2022, 08:12 last edited by JonB
                            #13

                            @sandip-nandwana
                            "Shows" how? What is your setNameFilterDisables() setting?

                            Did you at least try resetting setRootPath() to see whether it is a caching/updating issue?

                            S 1 Reply Last reply 20 Sept 2022, 09:01
                            0
                            • J JonB
                              20 Sept 2022, 08:12

                              @sandip-nandwana
                              "Shows" how? What is your setNameFilterDisables() setting?

                              Did you at least try resetting setRootPath() to see whether it is a caching/updating issue?

                              S Offline
                              S Offline
                              sandip.nandwana
                              wrote on 20 Sept 2022, 09:01 last edited by sandip.nandwana
                              #14

                              @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                              "Shows" how? What is your setNameFilterDisables() setting?

                              Is false.

                              @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                              Did you at least try resetting setRootPath() to see whether it is a caching/updating issue?

                              I tried but it did not work.

                              below is the code.

                              m_fileSystemModel->setNameFilterDisables( false );
                              m_fileSystemModel->setFilter( QDir::Dirs | QDir::Files );
                              
                              filesFilter.append( "*.csv" );
                              filesFilter.append( "ScreenCapture" );
                              filesFilter.append( "*.png" );
                              
                              if( m_isCaseStarted == true )
                              {
                                  /** @note:
                                   * info - 1793:
                                   * invoking non-const member function symbol of class symbol on a temporary
                                   */
                                  filesFilter.append( m_logDirectory.split("/").last() );//lint !e1793
                              }
                              else
                              {
                                  filesFilter.append( "Log*" );
                                  filesFilter.append( "Case*" );
                              }
                              
                              m_fileSystemModel->setNameFilterDisables( false );
                              m_fileSystemModel->setNameFilters( filesFilter );
                              
                              ui->logDirectoryListView->setModel( m_fileSystemModel );
                              m_fileSystemModel->setRootPath( m_logDirectory.left( m_logDirectoryPosition ) );//lint !e534
                              ui->logDirectoryListView->setRootIndex( m_fileSystemModel->index( m_logDirectory.left( m_logDirectoryPosition ) ) );
                              
                              J 1 Reply Last reply 20 Sept 2022, 09:14
                              0
                              • S sandip.nandwana
                                20 Sept 2022, 09:01

                                @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                                "Shows" how? What is your setNameFilterDisables() setting?

                                Is false.

                                @JonB said in How can I change the Qtreeview directory(folder) icon for different folders?:

                                Did you at least try resetting setRootPath() to see whether it is a caching/updating issue?

                                I tried but it did not work.

                                below is the code.

                                m_fileSystemModel->setNameFilterDisables( false );
                                m_fileSystemModel->setFilter( QDir::Dirs | QDir::Files );
                                
                                filesFilter.append( "*.csv" );
                                filesFilter.append( "ScreenCapture" );
                                filesFilter.append( "*.png" );
                                
                                if( m_isCaseStarted == true )
                                {
                                    /** @note:
                                     * info - 1793:
                                     * invoking non-const member function symbol of class symbol on a temporary
                                     */
                                    filesFilter.append( m_logDirectory.split("/").last() );//lint !e1793
                                }
                                else
                                {
                                    filesFilter.append( "Log*" );
                                    filesFilter.append( "Case*" );
                                }
                                
                                m_fileSystemModel->setNameFilterDisables( false );
                                m_fileSystemModel->setNameFilters( filesFilter );
                                
                                ui->logDirectoryListView->setModel( m_fileSystemModel );
                                m_fileSystemModel->setRootPath( m_logDirectory.left( m_logDirectoryPosition ) );//lint !e534
                                ui->logDirectoryListView->setRootIndex( m_fileSystemModel->index( m_logDirectory.left( m_logDirectoryPosition ) ) );
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 20 Sept 2022, 09:14 last edited by JonB
                                #15

                                @sandip-nandwana
                                You are trying to use setNameFilters() to filter the directories/folders shown, is that right? I would not expect that to work in any situation, whether you set it initially or change it? The name filters are intended to be like *.txt to filter the files being shown within each directory, but not for the directory names themselves (else e.g. *.txt would never be useful). Is that your situation?

                                If so, I think you need to find another approach, writing your own filter for directory names. You might interpose a QSortFilterProxyModel for this. There is an example in the accepted solutuon at e.g. https://stackoverflow.com/a/56627595/489865. It's a bit old but looks OK, use setFilterRegularExpression() instead of setFilterRegExp().

                                I am not sure whether that example will mean that your your whole tree will only display the (filtered) directories now, and nothing else. If you have to be smarter I think you will need to: subclass QSortFilterProxyModel, override QSortFilterProxyModel::filterAcceptsRow(), implement your logic there. Use that instead of setting a regular expression filter on the proxy so that you can implement logic to test for the item type to see whether it is a directory or a file, since you want these to behave differently.

                                1 Reply Last reply
                                0

                                3/15

                                16 Sept 2022, 11:50

                                12 unread
                                • Login

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