neowinston
neowinston

Reputation: 7764

Wrong text alignment in row using QTreeView

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.

Upvotes: 5

Views: 2037

Answers (2)

ekhumoro
ekhumoro

Reputation: 120808

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

Upvotes: 3

Dmitry Sazonov
Dmitry Sazonov

Reputation: 9014

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

Upvotes: 0

Related Questions