我有一个qtableview,允许用户编辑单元格中的值并更新底层模型。这适用于小型号(行*列大小为100到500)但在>处无法管理1000和我的一些模型可以变得很大(> 20000)。我想让用户看到模型的子区域并在tableview中浏览模型。但我认为没有哪种方法可以让我这样做,除非我建立自己的方法。我错过了吗?隐藏和显示模型的大部分是最好的方法吗? (我在想4个按钮可以向上/向下和向右分页数据(模型)
答案 0 :(得分:1)
如果您只想获得模型的一部分,可以使用代理,在这种情况下,我们创建一个继承自QAbstractProxyModel
的类:
class SubProxyModel(QAbstractProxyModel):
def __init__(self, x, y, nrows, ncols, parent=None):
QAbstractProxyModel.__init__(self, parent)
self._x = x
self._y = y
self._rows = nrows
self._cols = ncols
def setX(self, x):
self._x = x
self.modelReset.emit()
def setY(self, y):
self._y = y
self.modelReset.emit()
def setNRows(self, nrows):
self._rows = nrows
self.modelReset.emit()
def setNCols(self, ncols):
self._cols = ncols
self.modelReset.emit()
def parent(self, child):
return QModelIndex()
def rowCount(self, parent=QModelIndex()):
return self._rows
def columnCount(self, parent=QModelIndex()):
return self._cols
def mapToSource(self, proxyIndex):
r = proxyIndex.row()
c = proxyIndex.column()
nr = r + self._x
nc = c + self._y
return self.sourceModel().index(nr, nc)
def mapFromSource(self, sourceIndex):
ix = QModelIndex()
if sourceIndex.isValid():
r = sourceIndex.row()
c = sourceIndex.column()
nr = r - self._x
nc = c - self._y
ix = self.index(nr, nc)
return ix
def index(self, row, column, parent=QModelIndex()):
return self.createIndex(row, column)
示例:
class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
xoffset, yoffset, nRows, nCols = 3, 4, 5, 6
self.xSpinBox = QSpinBox()
self.rowsSpinBox = QSpinBox()
self.ySpinBox = QSpinBox()
self.colsSpinBox = QSpinBox()
glay = QGridLayout()
glay.addWidget(QLabel("x offset:"), 0, 0)
glay.addWidget(self.xSpinBox, 0, 1)
glay.addWidget(QLabel("number of rows:"), 0, 2)
glay.addWidget(self.rowsSpinBox, 0, 3)
glay.addWidget(QLabel("y offset:"), 1, 0)
glay.addWidget(self.ySpinBox, 1, 1)
glay.addWidget(QLabel("number of cols:"), 1, 2)
glay.addWidget(self.colsSpinBox, 1, 3)
lay = QVBoxLayout(self)
lay.addLayout(glay)
self.table = QTableView()
model = QStandardItemModel(1000, 1000)
for i in range(model.rowCount()):
for j in range(model.columnCount()):
model.setItem(i, j, QStandardItem(f"{i}{j}"))
proxy = SubProxyModel(xoffset, yoffset, nRows, nCols)
self.xSpinBox.setValue(xoffset)
self.ySpinBox.setValue(yoffset)
self.rowsSpinBox.setValue(nRows)
self.colsSpinBox.setValue(nCols)
proxy.setSourceModel(model)
self.table.setModel(proxy)
lay.addWidget(self.table)
self.xSpinBox.valueChanged.connect(self.onXValueChanged)
self.ySpinBox.valueChanged.connect(self.onYValueChanged)
self.rowsSpinBox.valueChanged.connect(self.onRowsValueChanged)
self.colsSpinBox.valueChanged.connect(self.onColsValueChanged)
self.xSpinBox.setMaximum(model.rowCount() - 1)
self.ySpinBox.setMaximum(model.columnCount() - 1)
def onRowsValueChanged(self, value):
self.table.model().setNRows(value)
def onColsValueChanged(self, value):
self.table.model().setNCols(value)
def onXValueChanged(self, value):
self.rowsSpinBox.setMaximum(self.table.model().sourceModel().rowCount() - value)
self.table.model().setX(value)
def onYValueChanged(self, value):
self.colsSpinBox.setMaximum(self.table.model().sourceModel().columnCount() - value)
self.table.model().setY(value)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
在以下link中,您可以找到完整的示例: