Results 1 to 13 of 13

Thread: how to browse images in thumbnail view?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: how to browse images in thumbnail view?

    Yeah, I'm actually using a QGraphicsView and a QGraphicsPixmapItem
    Because I know there are others out there with the same problem I'll post the code I have so far (still haven't implemented it using QImage though)

    In PyQt

    Qt Code:
    1. import PyQt4
    2. from PyQt4 import QtGui
    3. from PyQt4 import QtCore
    4. from PyQt4.QtCore import Qt
    5. from PyQt4.QtCore import SIGNAL
    6.  
    7. class QdGraphicsPixmapItem(QtGui.QGraphicsPixmapItem):
    8. def __init__(self, image, parent=None):
    9. QtGui.QGraphicsPixmapItem.__init__(self, parent)
    10. self._image = image
    11. self._loaded = False
    12. self.setFlags(self.flags() | QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsMovable )
    13.  
    14. def paint(self, painter, styleopt, widget):
    15. if not self._loaded:
    16. self._loaded = True
    17. pixmap = QtGui.QPixmap(self._image)
    18. thumb = pixmap.scaled(200, 200, Qt.KeepAspectRatio, Qt.SmoothTransformation)
    19. self.setPixmap(QtGui.QPixmap(self._image).scaled(200, 200))
    20. QtGui.QGraphicsPixmapItem.paint(self, painter, styleopt, widget)
    21.  
    22. class QdThumbnailView(QtGui.QGraphicsView):
    23. def __init__(self, images, parent=None):
    24. QtGui.QGraphicsView.__init__(self, parent)
    25. self._scene = QtGui.QGraphicsScene()
    26.  
    27. total = len(images)
    28. current = 0
    29. for image in images:
    30. #print current, 'of', total, os.path.basename(image)
    31. if os.path.isdir(image) or os.path.basename(image).startswith('.'):
    32. continue
    33. current += 1
    34.  
    35. item = QdGraphicsPixmapItem(image, current)
    36. item.setPos(0, 200*current)
    37. self._scene.addItem(item)
    38.  
    39. self._scene.setSceneRect(0, 0, 200, 200*current)
    40. self.setScene(self._scene)
    To copy to clipboard, switch view to plain text mode 

    You can imagine using a pool of background threads to load the thumbnails instead of loading them on demand. Thats where the QImage comes into play

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to browse images in thumbnail view?

    I'm writing an application that displays many thumbnails (as QLabels in a grid layout).
    Some more hints for the background loading + scaling is to

    • Use exiv2 to extract embedded thumbnails in e.g. digital camera files
    • store computed thumbnails in a database that can be shared with other applications

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,371
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to browse images in thumbnail view?

    Using QLabels to display many thumbnails is a big resource hog. All the images have to be kept in the server memory all the time and layouts need to be recalculated every time you add a new image. Using Item Views or Graphics View is much more resource efficient.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to browse images in thumbnail view?

    Does Graphics View then store its data somewhere else and transfer it over to the server when needed? Won't that make it slower?

  5. #5
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: how to browse images in thumbnail view?

    I'm using a graphics view however I want to use a flow layout which I dont believe exists for QGraphicsPixmapItems so I'm thinking I'm going to have to implement that myself... which seems like a daunting task.

    And so we're all on the same page I want the GraphicsView thumbnail viewer to have vertical scrollbars but no horizontal ones. The thumbnails would show up in rows, say 4 to a row and as you zoom in the icons would resize and eventually you'd have 3 to a row... etc

    I'm pretty sure there are other people out there looking to do the same exact thing since thats exactly how it works in thumbnail view in nautilus, konqueror, finder, etc. Basically any file browser that supports thumbnail views.

    Since konqueror is a Qt app its probably the best example... though unfortunately no one else can use it because its so KDE specific. I was hoping that we could find a Qt example of something like this in the 4.5 examples but no dice.

    In general it seems like it would be an awesome widget to have in say source forge or something with SIP bindings and all. Too bad Qt doesn't support a community of user made widgets in their git repos. The Qt labs examples are a great start but only the guys at Qt seem to be able to contribute.

  6. #6
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: how to browse images in thumbnail view?

    Just an update...

    I did some digging into the replacement for Konqueror, "Dolphin", and it appears to be using an item view for its thumbnail view (a KCategorizedView which is derived from a QListView). At least as far as I can tell it doesnt appear to be taking advantage of the QGraphicsView.

    I'm assuming they're using that because they have a QDirModel driving it.

    It sure would be nice to have an open source thumbnail viewer that takes advantage of the QGraphicsView. Not that my code is all that fantastic, but if I make some headway on one I'll post some stuff back here at the very least.

  7. #7
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: how to browse images in thumbnail view?

    Ok, as promised I have some code that will hopefully get some people started, its attached as a PyQt example

    I apologize for the lack of comments and docstrings but this was more of a mockup than anything else

    Here's how to use:

    Qt Code:
    1. app = QtGui.QApplication([])
    2. widget = QdContactSheet()
    3. images = # list of images you want to view as thumbnails
    4. images.sort()
    5. widget.load(images)
    6.  
    7. widget.setWindowTitle("Contact Sheet")
    8. widget.resize(1000, 800)
    9. widget.show()
    10. app.exec_()
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  8. The following 2 users say thank you to chezifresh for this useful post:

    jfjf (9th February 2011), ksl871 (26th April 2012)

Similar Threads

  1. How to view images using QT
    By Pitso in forum Newbie
    Replies: 1
    Last Post: 16th April 2007, 14:00

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
  •  
Qt is a trademark of The Qt Company.