Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active February 16, 2018 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/9d548816ea96c899c3d6c87a5b185d2d to your computer and use it in GitHub Desktop.
Save eyllanesc/9d548816ea96c899c3d6c87a5b185d2d to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
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_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment