pyqt:向 QFileSystemModel 添加自定义列

Err*_*ric 2 python user-interface qt pyqt

我需要向 QFileSystemModel 添加一个额外的列。我在以下位置看到了答案:QT - 添加自己的列到 QFileSystemModel
有人会告诉我如何在 pyqt4 中正确定义子类吗?

Yoa*_*ann 5

您几乎可以复制粘贴 C++ 代码。这是pyqt的实现:

class YourSystemModel(QtGui.QFileSystemModel):

    def columnCount(self, parent = QtCore.QModelIndex()):
        return super(YourSystemModel, self).columnCount()+1

    def data(self, index, role):
        if index.column() == self.columnCount() - 1:
            if role == QtCore.Qt.DisplayRole:
                return QtCore.QString("YourText")
            if role == QtCore.Qt.TextAlignmentRole:
                return QtCore.Qt.AlignHCenter

        return super(YourSystemModel, self).data(index, role)
Run Code Online (Sandbox Code Playgroud)