Results 1 to 3 of 3

Thread: QFileIconProvider custom icon slow

  1. #1
    Join Date
    Apr 2021
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default QFileIconProvider custom icon slow

    I'm using a QFileIconProvider class to show image thumbnails in a QFileDialog by overriding the 'icon' method to load from a file to a QIcon.

    After implementing this, the file dialog is very slow in navigating through sub-folders and hangs for a few seconds before changing directory.

    C++ class is below.

    Why is the rendering being slow and how can I speed it up?

    Qt Code:
    1. QIcon CFileIconProvider::icon(const QFileInfo &info) const
    2. {
    3. const QString &path = info.absoluteFilePath();
    4.  
    5. // check if 'info' is a drive type
    6. if (path.isEmpty())
    7. return QFileIconProvider::icon(QFileIconProvider::Drive);
    8.  
    9. if (info.isFile()) {
    10. if (QFile(info.absoluteFilePath()).exists()) {
    11. // show file as icon
    12. QImage originalImage(info.absoluteFilePath());
    13.  
    14. if (!originalImage.isNull())
    15. return QIcon(info.absoluteFilePath());
    16. }
    17. }
    18.  
    19. // check if 'info' is a directory type
    20. if (info.isDir())
    21. return QFileIconProvider::icon(QFileIconProvider::Folder);
    22.  
    23. return QIcon();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef FILEICON_PROVIDER_H
    2. #define FILEICON_PROVIDER_H
    3.  
    4. #include <QFileIconProvider>
    5. #include <QObject>
    6. #include <QIcon>
    7.  
    8. class CFileIconProvider : public QFileIconProvider
    9. {
    10.  
    11. public:
    12. CFileIconProvider();
    13. QIcon icon(const QFileInfo &info) const override;
    14.  
    15. };
    16.  
    17. #endif // FILEICON_PROVIDER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileIconProvider custom icon slow

    Hi,
    I think in this part
    Qt Code:
    1. if (info.isFile()) {
    2. if (QFile(info.absoluteFilePath()).exists()) {
    3. // show file as icon
    4. QImage originalImage(info.absoluteFilePath());
    5.  
    6. if (!originalImage.isNull())
    7. return QIcon(info.absoluteFilePath());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    you read the file two times, once to create the QImage, and then again to create the QIcon for the return.
    It would also probably help to cache the created icons so that you only have to create them once.

    Ginsengelf

  3. #3
    Join Date
    Apr 2021
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QFileIconProvider custom icon slow

    Thanks Ginsengelf,

    You were right the method was rending the file twice in the loop, which also was taking a lot of time to load into a QIcon.

    As per suggestion, I created a cache of icons that were rendered in a seperate thread in order to not be blocking, hash map was defined via:

    Qt Code:
    1. QMap<QString, QIcon>* iconCache;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QFileIconProvider does not work on MacOs
    By viktorkup in forum Qt Programming
    Replies: 0
    Last Post: 26th July 2021, 15:39
  2. QSystemTray Icon custom size
    By Rajesh.Rathod in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2013, 12:51
  3. QFileIconProvider and QFileDialog
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 27th October 2011, 18:45
  4. Replies: 1
    Last Post: 18th July 2011, 15:24
  5. QFileIconProvider problem
    By rittchat in forum Qt Programming
    Replies: 5
    Last Post: 29th November 2010, 04:02

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.