1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import sys
from PyQt4 import QtGui, QtCore, uic
class myModel(QtCore.QAbstractTableModel):
def __init__(self, myData = [[]], headers = [], parent = None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.myData = myData
self.headers = headers
def rowCount(self, parent):
return len(self.myData)
def columnCount(self, parent):
return len(self.myData[0])
def data(self, index, role):
row = index.row()
column = index.column()
value = self.myData[row][column]
return value
def insertRows(self, position, rows, parent = QtCore.QModelIndex()):
print('Inserting...')
self.beginInsertRows(parent, position, position + rows - 1)
defaultValues = ['Test' for i in range(self.columnCount(None))]
self.myData.insert(position, defaultValues)
self.endInsertRows()
return True
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
tableView = QtGui.QTableView()
tableView.show()
tableView.setGeometry(100,100,700,300)
rowCount = 4
columnCount = 6
headers = ["J" + str(i) for i in range(columnCount)]
tableData = [ [ str(i*j) for i in range(columnCount)] for j in range(rowCount)]
model = myModel(tableData, headers)
model.insertRows(0, 1)
tableView.setModel(model)
sys.exit(app.exec_()) |
Partager