0

そこで、QListView で画像を表示する方法を見つけました。

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):
        if index.isValid() and role == QtCore.Qt.DecorationRole:
            return QtGui.QIcon(QtGui.QPixmap(self.listdata[index.row()]))
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            return QtCore.QVariant(os.path.splitext(os.path.split(self.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 = "/usr/test1/Desktop"
        # create table
        list_data = []
        philes = os.listdir(crntDir)
        for phile in philes:
            if phile.endswith(".png"):
                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_())

しかし今、すべての画像を同じサムネイルサイズで表示する方法が必要であり、ファイル名はそれらを移動せず、ファイル名は次の行に折り返す必要があります!!! どうすればそれを達成できますか。

4

2 に答える 2

0

Qt のサムネイルのアスペクト比は、 のscaled() メソッドによって制御されQPixmapます。このメソッドには、サムネイルのサイズを制御するオプションも用意されています。size元のピックスマップでこのメソッドを呼び出すと、指定されたとを持つ新しいピックスマップが返されますAspectRatioMode

于 2013-12-04T16:14:09.563 に答える