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. Thumbnails in QFileDialog
Forum Updated to NodeBB v4.3 + New Features

Thumbnails in QFileDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 827 Views 2 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
    scrand
    wrote on last edited by
    #1

    Is there an easy way to make the QFileDialog show image thumbnails? (These are for common formats, like JPG and PNG). I have googled this extensively, and I cant find a good solution. If I run my code on Windows, it uses the Windows native file dialog, which has thumbnails. But if I run it on Linux, I don't get thumbnails. There are thumbnails in the file browsers (e.g., Nemo or Dolphin), so they exist. But in the "Open File" dialog, I just get gray rectangles. Is there an easy option I'm missing that will give me thumbnails? (And if not, is there some deep design philosophy reason this basic functionality is missing from QFileDialog?)

    If this isn't builtin to QFileDialog (for whatever reason), is there an easy way to write a QStyledItemDelegate that will display a thumbnail? (I have little knowledge/experience with delegates, other than I know that they exist).

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which Linux distribution is it ?
      Which desktop environment ?
      Are you using the distribution provided Qt ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Currently I'm on Linux Mint with a Plasma desktop. But I see the same issue on other configurations, like Mint with Cinnamon and Fedora with Gnome. The built-in QFileDialog just seems to lack support for thumbnails.

        Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The QStyledItemDelegate road it is then.

          As an educated guess, I would say that the quick way is to return the content of the image for the decoration role.

          For the long run, you should check if you have a thumbnail that has been generated for the file and use that.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            scrand
            wrote on last edited by scrand
            #5

            So I've learned a few things. One, I was using "ssh -X" into my dev account (which is on Plasma) from a Plasma workspace. I got the Qt dialog from that. But if I physically login to the dev account on Plasma, it uses the Plasma dialog, which has thumbnails. So that's something. But it looks like anything GNOME-based still doesn't work.

            I think I should be able to get the path and file name from the QModelIndex. And Linux caches thumbnails in ~/.cache/thumbnails, and uses an MD5 sum of the full path and filename as the name of the thumbnail. So I should be able to figure out the correct filename for a thumbnail, and use that as my thumbnail if it exists.

            So not really knowing what I'm doing, it looks like my approach is:

            1. Subclass QStyledItemDelegate, and get the thumbnail.
            2. Subclass QFileDialog and use the delegate.

            Delegate class:

            class ThumbnailDelegate : public QStyledItemDelegate
            {
                Q_OBJECT
            public:
                explicit ThumbnailDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent){
                        QString sFile, sThumbnail;
                        sFile = getFilenameFromModelIndex();
                        sThumbnail = getThumbnailFilename(sFile);
                        if(QFileInfo::exists(sThumbnail))
                        {
                            pxThumbnail = sThumbnail;
                            //Make the DisplayRole use pxThumbnail
                        }
                };
            
            protected:
                QString getFilenameFromModelIndex(void); //!< Extracts the full path and filename of the file from the QModelIndex
                QString getThumbnailFilename(QString filename); //!< Computes thumbnail filename from full path.
            
                QPixmap pxThumbnail; //!< Pixmap of the thumbnail
            };
            

            File dialog class:

            class ThumbnailFileDialog : public QFileDialog
            {
                Q_OBJECT
            public:
                explicit ThumbnailFileDialog(QWidget *parent = nullptr) : QFileDialog(parent){
                    delegate = new ThumbnailDelegate(this);
                    setItemDelegate(delegate);
                }
            
            protected:
                ThumbnailDelegate *delegate;
            
            };
            

            But I can't figure out how I:

            //Make the DisplayRole use pxThumbnail
            

            Do I need to re-implement "paint()"? Or is there a simpler way to do that?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              No need for the QFileDialog subclass, just set the delegate on it.

              As for your delegate, that's correct you need to implement the paint method.

              What you are currently doing in the constructor is wrong. You don't have one delegate per file, it's one delegate period.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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