سؤال

I have a QTreeView (with PyQt4) with custom and resized icons using the code below, but the Size column is displaying a wrong alignment/position, like so:

enter image description here

        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)

I've searched the http://pyqt.sourceforge.net/Docs/PyQt4/qtreeview.html documentation for a possible fix, but couldn't find anything evident.

هل كانت مفيدة؟

المحلول

One way to fix this is to reimplement the model's data() method so that the value for the TextAlignmentRole always includes the AlignVCenter flag:

# 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

نصائح أخرى

view -> model -> setData( index, YOUR_VALUE, Qt::TextAlignmentRole )

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top