4

我有一个QTreeView(带有PyQt4)使用下面的代码自定义和调整大小的图标,但大小列显示错误的对齐/位置,如下所示:

在此处输入图像描述

        self.ui.treeView.setIconSize(QtCore.QSize(30,30))

        fileSystemModel = QtGui.QFileSystemModel(self.ui.treeView)
        custonIconProvider = CustomIconsProvider()
        fileSystemModel.setIconProvider(custonIconProvider)

        self.ui.treeView.setModel(fileSystemModel)
        self.ui.treeView.setRootIndex(fileSystemModel.setRootPath(forlderPath))

        self.ui.treeView.setColumnWidth(0, 250)
        self.ui.treeView.setColumnWidth(1, 70)
        self.ui.treeView.setColumnWidth(2, 70)

我在http://pyqt.sourceforge.net/Docs/PyQt4/qtreeview.html文档中搜索了可能的修复方法,但找不到任何明显的东西。

4

2 回答 2

3

解决此问题的一种方法是重新实现模型的data() 方法,以便 TextAlignmentRole 的值始终包含 AlignVCenter 标志:

# python3 or sip.setapi('QVariant', 2)

class FileSystemModel(QtGui.QFileSystemModel):
    def data(self, index, role):
        value = super(FileSystemModel, self).data(index, role)
        if role == QtCore.Qt.TextAlignmentRole and value is not None:
            value |= QtCore.Qt.AlignVCenter
        return value

# python2 or sip.setapi('QVariant', 1)

class FileSystemModel(QtGui.QFileSystemModel):
    def data(self, index, role):
        value = super(FileSystemModel, self).data(index, role)
        if role == QtCore.Qt.TextAlignmentRole and value.isValid():
            value = value.toInt()[0] | QtCore.Qt.AlignVCenter
        return value
于 2013-11-27T03:39:04.330 回答
0

视图 -> 模型 -> setData(索引,YOUR_VALUE,Qt::TextAlignmentRole)

于 2013-09-24T15:46:46.743 回答