Results 1 to 3 of 3

Thread: seting QImage size in QTableView column and sluggish resize of window or columnn size

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

    Question seting QImage size in QTableView column and sluggish resize of window or columnn size

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: seting QImage size in QTableView column and sluggish resize of window or columnn

    Every time the view asks for the Qt:ecorationRole you load a pixmap from a file, scale it, convert it to a QImage, and return it. None of those operations is cheap, some of them unnecessary. Take a look at QPixmapCache

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

    Default Re: seting QImage size in QTableView column and sluggish resize of window or columnn

    Quote Originally Posted by ChrisW67 View Post
    Every time the view asks for the Qt:ecorationRole you load a pixmap from a file, scale it, convert it to a QImage, and return it. None of those operations is cheap, some of them unnecessary. Take a look at QPixmapCache
    I tried this below, but it still seems to slow.
    Qt Code:
    1. def data(self, index, role):
    2.  
    3. if index.isValid() and role == QtCore.Qt.DecorationRole:
    4. row = index.row()
    5. column = index.column()
    6. value = self._listdata[row][column]
    7. key = "image:%s"% value
    8. pixmap = QtGui.QPixmap()
    9. # pixmap.load(value)
    10. if not QtGui.QPixmapCache.find(key, pixmap):
    11. pixmap=self.generatePixmap(value)
    12. QtGui.QPixmapCache.insert(key, pixmap)
    13. # pixmap.scaled(400,300, QtCore.Qt.KeepAspectRatio)
    14. return QtGui.QImage(pixmap)
    15.  
    16. if index.isValid() and role == QtCore.Qt.DisplayRole:
    17. row = index.row()
    18. column = index.column()
    19. value = self._listdata[row][column]
    20. fileName = os.path.split(value)[-1]
    21. return os.path.splitext(fileName)[0]
    22.  
    23. def generatePixmap(self, value):
    24. pixmap=QtGui.QPixmap()
    25. pixmap.load(value)
    26. pixmap.scaled(100, 120, QtCore.Qt.KeepAspectRatio)
    27. return pixmap
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 11th April 2011, 17:03
  2. Seting the size of plot
    By mikl in forum Qwt
    Replies: 1
    Last Post: 13th March 2011, 12:55
  3. Replies: 1
    Last Post: 29th April 2010, 06:21
  4. Replies: 0
    Last Post: 28th May 2009, 23:38
  5. Replies: 0
    Last Post: 14th February 2008, 13:14

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.