I want to display images of specific thumbnail and column height width to fit accordingly keeping the text i.e. the filename underneath the image.

Qt Code:
  1. import sys
  2. import os
  3.  
  4. from PyQt4 import QtGui, QtCore
  5.  
  6. class MyListModel(QtCore.QAbstractTableModel):
  7. def __init__(self, datain, col ,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. self._col = col
  13.  
  14.  
  15. def colData(self, section, orientation, role):
  16. if role == QtCore.Qt.DisplayRole:
  17. if orientation == QtCore.Qt.Horizontal:
  18. return QtCore.QString("Images")
  19. else:
  20. return QtCore.QString(os.path.splitext(self._listdata[section])[-1][1:].upper())
  21.  
  22. def rowCount(self, parent=QtCore.QModelIndex()):
  23. return len(self._listdata)
  24.  
  25. def columnCount(self, parent):
  26. return self._col
  27.  
  28. def data(self, index, role):
  29.  
  30. if role == QtCore.Qt.EditRole:
  31. row = index.row()
  32. column = index.column()
  33. fileName = os.path.split(self._listdata[row][column])[-1]
  34. return fileName
  35.  
  36. if role == QtCore.Qt.ToolTipRole:
  37. row = index.row()
  38. column = index.column()
  39. fileName = os.path.split(self._listdata[row][column])[-1]
  40. return QtCore.QString(fileName)
  41.  
  42. if index.isValid() and role == QtCore.Qt.DecorationRole:
  43. row = index.row()
  44. column = index.column()
  45. value = self._listdata[row][column]
  46. pixmap = QtGui.QPixmap()
  47. # pixmap.scaled(400,300, QtCore.Qt.KeepAspectRatio)
  48. pixmap.load(value)
  49. return QtGui.QImage(pixmap).scaled(120,100)
  50.  
  51. if index.isValid() and role == QtCore.Qt.DisplayRole:
  52. row = index.row()
  53. column = index.column()
  54. value = self._listdata[row][column]
  55. fileName = os.path.split(value)[-1]
  56. return os.path.splitext(fileName)[0]
  57.  
  58. def flags(self, index):
  59. return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
  60.  
  61. def setData(self, index, value, role=QtCore.Qt.EditRole):
  62. if role == QtCore.Qt.EditRole:
  63. row = index.row()
  64. column = index.column()
  65. newName = os.path.join(str(os.path.split(self._listdata[row][column])[0]), str(value.toString()))
  66. self.__renameFile(self._listdata[row][column], newName)
  67. self._listdata[row][column] = newName
  68. self.dataChanged.emit(index, index)
  69. return True
  70. return False
  71.  
  72. def __renameFile(self, fileToRename, newName):
  73. try:
  74. os.rename(str(fileToRename), newName)
  75. except Exception, err:
  76. print err
  77.  
  78.  
  79. class MyTableView(QtGui.QTableView):
  80. """docstring for MyTableView"""
  81. def __init__(self):
  82. super(MyTableView, self).__init__()
  83.  
  84. sw = QtGui.QDesktopWidget().screenGeometry(self).width()
  85. sh = QtGui.QDesktopWidget().screenGeometry(self).height()
  86. self.resize(sw, sh)
  87.  
  88. thumbWidth = 100
  89. thumbheight = 120
  90.  
  91. col = sw/thumbWidth
  92. self.setColumnWidth(thumbWidth, thumbheight)
  93. crntDir = "/Users/krystosan/Pictures"
  94. # create table
  95. list_data = []
  96. philes = os.listdir(crntDir)
  97. for phile in philes:
  98. if phile.endswith(".png") or phile.endswith("jpg"):
  99. list_data.append(os.path.join(crntDir, phile))
  100. _twoDLst = convertToTwoDList(list_data, col)
  101. lm = MyListModel(_twoDLst, col, self)
  102. self.setModel(lm)
  103. self.show()
  104.  
  105. def convertToTwoDList(l, n):
  106. return [l[i:i+n] for i in range(0, len(l), n)]
  107.  
  108. if __name__ == '__main__':
  109. app = QtGui.QApplication(sys.argv)
  110. window = MyTableView()
  111. window.show()
  112. window.raise_()
  113. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 

and I don't understand why resizing the window or changing column height width is happening very slow.