#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' PyQt4 conversion of Qt Model-View Tutorial The current time is displayed and updated every second. NOTES: ===== A 'QTimer' is added to '__init__()' A 'slot', 'timerHit()', is added and connected to the timer The clock is created in 'data()' 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) from PyQt4.QtCore import (Qt, QAbstractTableModel, QModelIndex, QTimer, QTime, pyqtSlot) class MyModel(QAbstractTableModel): def __init__(self, parent=None): # initialise base class super(MyModel, self).__init__(parent) timer = QTimer(self) timer.setInterval(1000) # 1 second timer.timeout.connect(self.timerHit) timer.start() @pyqtSlot() def timerHit(self): topLeft = self.createIndex(0, 0) self.dataChanged.emit(topLeft, topLeft) def rowCount(self, index=QModelIndex()): return 2 def columnCount(self, index=QModelIndex()): return 3 def data(self, index, role): row = index.row() col = index.column() if role == Qt.DisplayRole: if (row == 0 and col == 0): return QTime.currentTime().toString() else: return None # main ======================================================================== if __name__ == '__main__': import sys app = QApplication(sys.argv) tv = QTableView() myModel = MyModel() tv.setModel(myModel) tv.setWindowTitle("A Clock inside a Cell") tv.show() sys.exit(app.exec_())
Page List
▼
Friday, January 27, 2012
Qt 4.8 Model/View Tutorial - Part 3
The following code is based on the Qt Model/View Tutorial - 2.3 A Clock Inside a Table Cell.