Results 1 to 3 of 3

Thread: Displaying Images in QListView with same thumbnail size keeping aspect ratio

  1. #1
    Join Date
    Nov 2012
    Posts
    35
    Thanks
    1
    Thanked 7 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Displaying Images in QListView with same thumbnail size keeping aspect ratio

    How do I display images in QListView of size of standard thumbnail size like 250 x 200 , and file nametext should wrap to next line , right now the images displayed are very small and are not positioned under each other in proper row column order.

    Qt Code:
    1. import sys
    2. import os
    3.  
    4. from PyQt4 import QtGui, QtCore
    5.  
    6. class MyListModel(QtCore.QAbstractListModel):
    7. def __init__(self, datain, parent=None, *args):
    8. """ datain: a list where each item is a row
    9. """
    10. QtCore.QAbstractListModel.__init__(self, parent, *args)
    11. self.listdata = datain
    12.  
    13. def rowCount(self, parent=QtCore.QModelIndex()):
    14. return len(self.listdata)
    15.  
    16. def data(self, index, role):
    17. s = QtCore.QSize(250, 200)
    18. if index.isValid() and role == QtCore.Qt.DecorationRole:
    19. return QtGui.QIcon(QtGui.QPixmap(self.listdata[index.row()]).scaled(s))
    20. if index.isValid() and role == QtCore.Qt.DisplayRole:
    21. return QtCore.QVariant(os.path.splitext(os.path.split(self.listdata[index.row()])[-1])[0])
    22. else:
    23. return QtCore.QVariant()
    24.  
    25. class MyListView(QtGui.QListView):
    26. """docstring for MyListView"""
    27. def __init__(self):
    28. super(MyListView, self).__init__()
    29. # show in Icon Mode
    30. self.setViewMode(QtGui.QListView.IconMode)
    31. crntDir = "/Users/userName/Pictures/"
    32. # create table
    33. list_data = []
    34. philes = os.listdir(crntDir)
    35. for phile in philes:
    36. if phile.endswith(".png") or phile.endswith("jpg"):
    37. list_data.append(os.path.join(crntDir, phile))
    38. lm = MyListModel(list_data, self)
    39. self.setModel(lm)
    40. self.show()
    41.  
    42. if __name__ == '__main__':
    43. app = QtGui.QApplication(sys.argv)
    44. window = MyListView()
    45. window.show()
    46. window.raise_()
    47. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Displaying Images in QListView with same thumbnail size keeping aspect ratio

    You could try setUniformItemSize() and/or setGridSize() for the positioning problem.
    And setIconSize() for the size problem.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2012
    Posts
    35
    Thanks
    1
    Thanked 7 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Displaying Images in QListView with same thumbnail size keeping aspect ratio

    Quote Originally Posted by anda_skoa View Post
    You could try setUniformItemSize() and/or setGridSize() for the positioning problem.
    And setIconSize() for the size problem.

    Cheers,
    _
    now i m using QTableView, what do i use for that here is the code

Similar Threads

  1. Replies: 1
    Last Post: 4th August 2012, 06:55
  2. Printing and Aspect ratio
    By cpt4 in forum Qwt
    Replies: 9
    Last Post: 30th September 2011, 15:28
  3. How to get aspect ratio on selection with rubberBand?
    By suseway in forum Qt Programming
    Replies: 6
    Last Post: 25th October 2010, 09:29
  4. keeping aspect ratio while resizing
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 21:12
  5. Preserving aspect ratio of image
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 14:39

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.