5

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
neowinston
  • 7,584
  • 10
  • 52
  • 83

2 Answers2

3

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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

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

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • I don't know python, but I specified methods that should be called. I think that it is clear, what to do. If someone downvoted - please, add comments, why. – Dmitry Sazonov Sep 25 '13 at 07:03
  • I didn't down voted your answer, it was someone else. I understood your code and tried it in Python, but I didn't get the desired effect. – neowinston Sep 25 '13 at 14:20
  • I tried this code: fileSystemModel.setData(QtCore.QModelIndex(), QtCore.QVariant(QtCore.Qt.AlignLeft), QtCore.Qt.TextAlignmentRole) – neowinston Sep 25 '13 at 14:37
  • You are passing invalid `QModelIndex`. You should path valid index for each cell. Or it is better to subclass your model and reimplement `data` method, that will always return prefferable value for "size" column. – Dmitry Sazonov Sep 25 '13 at 14:42
  • Maybe I'll have to call it in a loop for each item, like this links suggests: https://bugreports.qt-project.org/browse/QTBUG-595 – neowinston Sep 25 '13 at 14:42
  • Yes, I was thinking that my QtCore.QModelIndex() call was wrong from the start. – neowinston Sep 25 '13 at 14:44
  • Loop is not good solution, because you should somehow track model for modifications and set alignment for each new items. Much better to subclass model ;) – Dmitry Sazonov Sep 25 '13 at 14:45
  • I'll try to subclass it. Thanks for your thoughts and time! – neowinston Sep 25 '13 at 14:50
  • No problem. Btw, there are special buttons/arrows for "thanks" ;) – Dmitry Sazonov Sep 25 '13 at 15:28
  • Here http://pyqt.sourceforge.net/Docs/PyQt4/qfilesystemmodel.html doesn't say that the class QFileSystemModel has a TextAlignmentRole. – neowinston Sep 25 '13 at 17:22
  • It's a not class property. It's attribute of ANY item. It's located in global Qt namespace. – Dmitry Sazonov Sep 25 '13 at 17:28