实现QAbstractProxyModel方法

时间:2018-03-30 00:58:03

标签: python pyqt pyqt5 qidentityproxymodel qabstractproxymodel

我有一个QSqlTableModel,大概有这个结构:

| ID |  Name  |
---------------
|  0 |  Xxxx  |
|  2 |  Yyyy  |
|  5 |  Zzzz  |

如您所见,ID(它们是唯一的)是顺序的,但可以跳过;可用范围始终为0到1023.我需要创建一个填充1024行间隙的表,而不更改源模型布局。最终结果将是这样的:

|  0 |  Xxxx  |
|    |        |
|  2 |  Yyyy  |
|    |        |
|    |        |
|  5 |  Zzzz  |
      ...
|1023|  Xyz   |

“空白”项目不可编辑,但用户可以使用拖放操作重新排序(将在内部与SQL模型连接实现)并添加/删除项目,同时始终保持表格大小到1024行。

我尝试过实现QAbstractProxyModel,但是我遇到了一些问题。例如,不遵守标志,每个项目都不可编辑,也不可选择。即使我特意返回ItemIsEnabled|ItemIsSelectable|ItemIsEditable,也没有任何变化。此外,单击项目会为标题突出显示产生奇怪的结果。

我想我对mapToSource/mapFromSource做错了,但我不确定。

这是我使用QStandardItemModel而不是QSqlTableModel(结果行为相同)使用4行,而代理显示6行的示例。

class BaseModel(QtGui.QStandardItemModel):
    def __init__(self):
        QtGui.QStandardItemModel.__init__(self)
        for id, name in [(1, 'One'), (2, 'Two'), (3, 'Three'), (5, 'Five')]:
            idItem = QtGui.QStandardItem()
            idItem.setData(id, QtCore.Qt.DisplayRole)
            nameItem = QtGui.QStandardItem(name)
            self.appendRow([idItem, nameItem])


class ProxyModel(QtCore.QAbstractProxyModel):
    def data(self, index, role):
        source = self.mapToSource(index)
        if source.isValid():
            return source.data(role)
        return None

    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
            return str(section + 1)
        return QtCore.QAbstractProxyModel.headerData(self, section, orientation, role)

    def setData(self, index, value, role):
        return self.sourceModel().setData(self.mapToSource(index), value, role)

    def index(self, row, column, parent=None):
        res = self.sourceModel().match(self.sourceModel().index(0, 0), QtCore.Qt.DisplayRole, row, flags=QtCore.Qt.MatchExactly)
        if res:
            return res[0].sibling(res[0].row(), column)
        return self.createIndex(row, column)

    def parent(self, index):
        return self.sourceModel().index(index.row(), index.column()).parent()

    def flags(self, index):
        source = self.mapToSource(index)
        if source.isValid():
            return source.flags()
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEditable

    def rowCount(self, index):
        return 6

    def columnCount(self, index):
        return 2

    def mapToSource(self, index):
        res = self.sourceModel().match(self.sourceModel().index(0, 0), QtCore.Qt.DisplayRole, index.row(), flags=QtCore.Qt.MatchExactly)
        if res:
            return res[0].sibling(res[0].row(), index.column())
        return QtCore.QModelIndex()

    def mapFromSource(self, index):
        if index.row() < 0:
            return QtCore.QModelIndex()
        row = self.sourceModel().index(index.row(), 0).data()
        return self.createIndex(row, index.column())


class Win(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        layout = QtWidgets.QGridLayout()
        self.setLayout(layout)
        self.model = BaseModel()
        self.proxy = ProxyModel()
        self.proxy.setSourceModel(self.model)
        table = QtWidgets.QTableView()
        table.setModel(self.proxy)
        layout.addWidget(table)

1 个答案:

答案 0 :(得分:2)

没有必要实施QAbstractProxyModel,在这种情况下使用QIdentityProxyModel就足够了:

class ProxyModel(QtCore.QIdentityProxyModel):
    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
            return str(section + 1)
        return QtCore.QIdentityProxyModel.headerData(self, section, orientation, role)

    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        if index.column() == 0:
            if not (0 <= int(value) < self.rowCount()) :
                return False
        return QtCore.QIdentityProxyModel.setData(self, index, value, role)

    def rowCount(self, index=QtCore.QModelIndex()):
        return 6

    def index(self, row, column, parent=QtCore.QModelIndex()):
        return self.createIndex(row, column)

    def mapFromSource(self, sourceIndex):
        if sourceIndex.isValid()  and 0 <= sourceIndex.row() < self.rowCount():
            ix = self.sourceModel().index(sourceIndex.row(), 0)
            return self.index(int(ix.data()), sourceIndex.column())
        return QtCore.QModelIndex()

    def mapToSource(self, proxyIndex):
        res = self.sourceModel().match(self.sourceModel().index(0, 0), QtCore.Qt.DisplayRole, proxyIndex.row(), flags=QtCore.Qt.MatchExactly)
        if res:
            return res[0].sibling(res[0].row(), proxyIndex.column())
        return QtCore.QModelIndex()
相关问题