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