#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
PyQt4 conversion of Qt Model-View Tutorial
Add headers to a table view by overriding
headerData().
NOTES:
=====
Headers are shown/hidden using QTableView.verticalHeader().hide()
but they are defined in the model.
The Qt example only includes column headers, added row headers
and set row and column header text color to red.
last modified: 2012-01-27 jg
ref:
http://developer.qt.nokia.com/doc/qt-4.8/modelview.html#2-3-a-clock-inside-a-table-cell
'''
from PyQt4.QtGui import (QApplication, QTableView, QColor)
from PyQt4.QtCore import (Qt, QAbstractTableModel, QModelIndex)
class MyModel(QAbstractTableModel):
def __init__(self, parent=None): # initialise base class
super(MyModel, self).__init__(parent)
def rowCount(self, index=QModelIndex()):
return 2
def columnCount(self, index=QModelIndex()):
return 3
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if(orientation == Qt.Horizontal):
if section == 0: return "Column One"
elif section == 1: return "Column Two"
elif section == 2: return "Column Three"
elif(orientation == Qt.Vertical):
if section == 0: return "Row One"
elif section == 1: return "Row Two"
elif role == Qt.TextColorRole:
return QColor(Qt.red)
return None
def data(self, index, role):
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
return "Row {0}, Column {1}".format(row + 1,
col + 1)
else:
return None
# main ========================================================================
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
tv = QTableView()
myModel = MyModel()
tv.setModel(myModel)
tv.setWindowTitle("Adding Headers")
tv.show()
sys.exit(app.exec_())
Friday, January 27, 2012
Qt 4.8 Model/View Tutorial - Part 4
The following is based on code from the Qt 4.8 Model/View Tutorial - 2.4 Setting Up Headers for Columns and Rows.
Labels:
Qt Model/View Tutorial