PDA

View Full Version : Displaying Images in QListView with same thumbnail size keeping aspect ratio



krystosan
6th December 2013, 17:45
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.



import sys
import os

from PyQt4 import QtGui, QtCore

class MyListModel(QtCore.QAbstractListModel):
def __init__(self, datain, parent=None, *args):
""" datain: a list where each item is a row
"""
QtCore.QAbstractListModel.__init__(self, parent, *args)
self.listdata = datain

def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.listdata)

def data(self, index, role):
s = QtCore.QSize(250, 200)
if index.isValid() and role == QtCore.Qt.DecorationRole:
return QtGui.QIcon(QtGui.QPixmap(self.listdata[index.row()]).scaled(s))
if index.isValid() and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(os.path.splitext(os.path.split(sel f.listdata[index.row()])[-1])[0])
else:
return QtCore.QVariant()

class MyListView(QtGui.QListView):
"""docstring for MyListView"""
def __init__(self):
super(MyListView, self).__init__()
# show in Icon Mode
self.setViewMode(QtGui.QListView.IconMode)
crntDir = "/Users/userName/Pictures/"
# create table
list_data = []
philes = os.listdir(crntDir)
for phile in philes:
if phile.endswith(".png") or phile.endswith("jpg"):
list_data.append(os.path.join(crntDir, phile))
lm = MyListModel(list_data, self)
self.setModel(lm)
self.show()

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyListView()
window.show()
window.raise_()
sys.exit(app.exec_())

anda_skoa
6th December 2013, 19:08
You could try setUniformItemSize() and/or setGridSize() for the positioning problem.
And setIconSize() for the size problem.

Cheers,
_

krystosan
7th December 2013, 19:02
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 (http://codereview.stackexchange.com/questions/36843/my-first-attempt-of-modelview-programming-in-pyqt4)