# -*- coding: cp1251 -*-
from PyQt4 import QtGui, QtCore
import sys
class Model(QtCore.QAbstractTableModel):
def __init__(self, parent):
QtCore.QAbstractTableModel.__init__(self)
self.gui = parent
self.colLabels = ['Col1', 'Col2', 'Col3', 'Col4', 'Col5']
self.cached = [
['cell11','cell12','cell13','cell14','cell15',],
['cell21','cell22','cell23','cell24','cell25',],
['cell31','cell32','cell33','cell34','cell35',],
['cell41','cell42','cell43','cell44','cell45',],
['cell51','cell52','cell53','cell54','cell55',],
['cell61','cell62','cell63','cell64','cell65',],
['cell71','cell72','cell73','cell74','cell75',],
['cell81','cell82','cell83','cell84','cell85',],
]
def rowCount(self, parent):
return len(self.cached)
def columnCount(self, parent):
return len(self.colLabels)
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
elif role != QtCore.Qt.DisplayRole and role != QtCore.Qt.EditRole:
return QtCore.QVariant()
value = ''
if role == QtCore.Qt.DisplayRole:
row = index.row()
col = index.column()
value = self.cached[row][col]
return QtCore.QVariant(value)
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.colLabels[section])
return QtCore.QVariant()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
self.table = QtGui.QTableView(self)
self.model = Model(self.table)
self.table.setModel(self.model)
self.setCentralWidget(self.table)
class App(QtGui.QApplication):
def __init__(self, argv):
QtGui.QApplication.__init__(self, argv)
self.ui = MainWindow(None)
self.ui.show()
if __name__ == "__main__":
app = App(sys.argv)
sys.exit(app.exec_())