def __init__(self,persondata,headerdata,parent = None): QAbstractTableModel.__init__(self,parent) self.persondata = persondata self.headerdata = headerdata self._genderFormatter = LookupFormatter(CheckGender) self._mStatFormatter = LookupFormatter(CheckMaritalStatus) self._ageFormatter = DoBFormatter()
def __init__(self, persondata, headerdata, parent=None): QAbstractTableModel.__init__(self, parent) self.persondata = persondata self.headerdata = headerdata self._genderFormatter = LookupFormatter(CheckGender) self._mStatFormatter = LookupFormatter(CheckMaritalStatus) self._ageFormatter = DoBFormatter()
class PersonTableModel(QAbstractTableModel): ''' Model for use in the Person table view ''' def __init__(self,persondata,headerdata,parent = None): QAbstractTableModel.__init__(self,parent) self.persondata = persondata self.headerdata = headerdata self._genderFormatter = LookupFormatter(CheckGender) self._mStatFormatter = LookupFormatter(CheckMaritalStatus) self._ageFormatter = DoBFormatter() def rowCount(self, parent = QModelIndex()): return len(self.persondata) def columnCount(self, parent = QModelIndex()): return 11 def data(self,index, role): indexData = self.persondata[index.row()][index.column()] if not index.isValid(): return QVariant() elif role == Qt.DisplayRole: col = index.column() #Specify formatters for columns whose values are foreign keys if col == 5: #Gender formatter return self._genderFormatter.setDisplay(indexData) elif col == 6: #Current age calculation return self._ageFormatter.setDisplay(indexData) elif col == 7: return self._mStatFormatter.setDisplay(indexData) else: return QVariant(indexData) #For columns representing foreign keys then we need to pass the integer value to the editor elif role == Qt.EditRole: #Create QDate from Python date then pass it to QVariant constructor where applicable if index.column() == 6: if isinstance(indexData,QVariant): return QVariant(indexData) else: return QVariant(QDate(indexData)) else: return QVariant(indexData) elif role == Qt.BackgroundRole: if index.row() % 2 == 0: #Orange return QVariant(ALT_COLOR_EVEN) else: #Blue return QVariant(ALT_COLOR_ODD) else: return QVariant() def headerData(self,col,orientation,role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QVariant(self.headerdata[col]) return QVariant() def setData(self,index,value,role = Qt.EditRole): if index.isValid() and role == Qt.EditRole: self.persondata[index.row()][index.column()] = value self.dataChanged.emit(index,index) return True return False def flags(self,index): if not index.isValid(): return Qt.ItemIsEnabled return Qt.ItemIsEditable|Qt.ItemIsSelectable|Qt.ItemIsEnabled def insertRows(self,position,rows,parent = QModelIndex()): if position < 0 or position > len(self.persondata): return False self.beginInsertRows(parent, position, position + rows - 1) #Initialize column values for the new row initRowVals = ["" for c in range(self.columnCount())] for i in range(rows): self.persondata.insert(position,initRowVals) self.endInsertRows() return True def removeRows(self,position,count,parent = QModelIndex()): if position < 0 or position > len(self.persondata): return False self.beginRemoveRows(parent,position,position + count - 1) for i in range(count): del self.persondata[position] self.endRemoveRows() return True
class PersonTableModel(QAbstractTableModel): ''' Model for use in the Person table view ''' def __init__(self, persondata, headerdata, parent=None): QAbstractTableModel.__init__(self, parent) self.persondata = persondata self.headerdata = headerdata self._genderFormatter = LookupFormatter(CheckGender) self._mStatFormatter = LookupFormatter(CheckMaritalStatus) self._ageFormatter = DoBFormatter() def rowCount(self, parent=QModelIndex()): return len(self.persondata) def columnCount(self, parent=QModelIndex()): return 11 def data(self, index, role): indexData = self.persondata[index.row()][index.column()] if not index.isValid(): return QVariant() elif role == Qt.DisplayRole: col = index.column() #Specify formatters for columns whose values are foreign keys if col == 5: #Gender formatter return self._genderFormatter.setDisplay(indexData) elif col == 6: #Current age calculation return self._ageFormatter.setDisplay(indexData) elif col == 7: return self._mStatFormatter.setDisplay(indexData) else: return QVariant(indexData) #For columns representing foreign keys then we need to pass the integer value to the editor elif role == Qt.EditRole: #Create QDate from Python date then pass it to QVariant constructor where applicable if index.column() == 6: if isinstance(indexData, QVariant): return QVariant(indexData) else: return QVariant(QDate(indexData)) else: return QVariant(indexData) elif role == Qt.BackgroundRole: if index.row() % 2 == 0: #Orange return QVariant(ALT_COLOR_EVEN) else: #Blue return QVariant(ALT_COLOR_ODD) else: return QVariant() def headerData(self, col, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QVariant(self.headerdata[col]) return QVariant() def setData(self, index, value, role=Qt.EditRole): if index.isValid() and role == Qt.EditRole: self.persondata[index.row()][index.column()] = value self.dataChanged.emit(index, index) return True return False def flags(self, index): if not index.isValid(): return Qt.ItemIsEnabled return Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled def insertRows(self, position, rows, parent=QModelIndex()): if position < 0 or position > len(self.persondata): return False self.beginInsertRows(parent, position, position + rows - 1) #Initialize column values for the new row initRowVals = ["" for c in range(self.columnCount())] for i in range(rows): self.persondata.insert(position, initRowVals) self.endInsertRows() return True def removeRows(self, position, count, parent=QModelIndex()): if position < 0 or position > len(self.persondata): return False self.beginRemoveRows(parent, position, position + count - 1) for i in range(count): del self.persondata[position] self.endRemoveRows() return True