Python 查看Qtableview的子区域
我有一个qtableview,允许用户编辑单元格中的值并更新基础模型。这对于小型模型(行*列大小为100到500)来说效果很好,但在>1000时变得难以管理,我的一些模型可能会变得巨大(>20000)。我考虑允许用户查看模型的子区域,并在tableview中浏览模型。但我看不到任何方法允许我这样做,除非我建立自己的方法。我错过了吗?隐藏和显示模型的大部分是最好的方法吗?(我想到了4个按钮,可以上下左右翻页数据(模型)如果您只想获得模型的一部分,可以使用代理,在本例中,我们创建了一个继承自Python 查看Qtableview的子区域,python,pyqt,pyqt5,qtableview,qstandarditemmodel,Python,Pyqt,Pyqt5,Qtableview,Qstandarditemmodel,我有一个qtableview,允许用户编辑单元格中的值并更新基础模型。这对于小型模型(行*列大小为100到500)来说效果很好,但在>1000时变得难以管理,我的一些模型可能会变得巨大(>20000)。我考虑允许用户查看模型的子区域,并在tableview中浏览模型。但我看不到任何方法允许我这样做,除非我建立自己的方法。我错过了吗?隐藏和显示模型的大部分是最好的方法吗?(我想到了4个按钮,可以上下左右翻页数据(模型)如果您只想获得模型的一部分,可以使用代理,在本例中,我们创建了一个继承自QAbs
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_())
下面是完整的示例:上、下、左、右??这些模型不是图形,如果您要处理大量数据并且存在效率问题,请不要使用QStandarItemModel,创建您自己的模型。信息显示部分由QTableView处理,它经过优化以显示此类信息。你想做什么?说清楚-QTableView和QStandardItemModel的一般功能可以正常工作。我想看的是,只看N行数据,从基行X的偏移量开始,M列数据,从基列Y的偏移量开始。好的,现在我明白了,你想显示一个子模型:DPyQt4还是PyQt5?PyQt5是我现在使用的。