Results 1 to 13 of 13

Thread: how to browse images in thumbnail view?

  1. #1
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question how to browse images in thumbnail view?

    hi, there
    My application needs to browse thousands of images in thumbnail view. I tried QListWidget with icon mode but found it's extremely slow and memory consuming. Many image browser software like GWinView, which is using qt3, handles the problem nicely. How can I do this in qt4? The images are jpg or png format (I won't try other format at this time.) Here is the piece of code:

    Qt Code:
    1. while (myIterator.hasNext())
    2. {
    3. QString myFileName = myIterator.next();
    4. QFileInfo myFileInfo(myFileName);
    5. if (!myFileInfo.isFile()) continue;
    6. QPixmap myPixmap(myFileName);
    7. QListWidgetItem * mypItem = new QListWidgetItem(myFileInfo.baseName());
    8. mypItem->setIcon(myPixmap);
    9. mypItem->setData(Qt::UserRole,myFileName);
    10. listWgt1->addItem(mypItem);
    11. }
    12. listWgt1->sortItems(Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    The listWgt1 is a QListWidget set to icon mode. Comparing to Gwenview, the loading time is about twice longer and the scrolling is noticeably delayed (but tolerable). The scaling of thumbnails are fast. The memory consuming is much much larger than Gwenview.

    Is there any other approach that I can do the thumbview in qt4 fast and memory efficient? (for several thousands of images). Thanks for help.

    zl2k
    Last edited by jpn; 13th September 2008 at 18:52. Reason: changed [quote] to [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    Load and scale images in the background, for instance using worker thread(s).

  3. #3
    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?

    whats the best algorithm to use for a background thread since you can't create a QPixmap outside the main thread?

  4. #4
    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 love answering my own question. For those of you who are lazy like me, the simple answer is that you can use QImage outside of the main thread. So instead of using QPixmap use a QImage

    See the following thread for more info:

    http://www.qtcentre.org/forum/f-newb...ight=thumbnail

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    You can create QImages in worker threads. Currently the best solution is to use QtConcurrent.
    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.


  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to browse images in thumbnail view?

    You can also try using simply QLabels in grid layout...

    or you can also give a thought to graphicsview framework,,, zooming will be faster in graphics view since you can scale the whole view, and not just single items...

  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?

    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

  8. #8
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  10. #10
    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?

  11. #11
    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.

  12. #12
    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.

  13. #13
    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

  14. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.