Showing posts with label Misc.. Show all posts
Showing posts with label Misc.. Show all posts

Wednesday, October 23, 2013




class MainForm(QDialog):
def __init__(self):
super(MainForm, self).__init__()
self.assetModel = QSqlRelationalTableModel(self)
self.assetModel.setTable("assets")
self.assetModel.setRelation(CATEGORYID,
QSqlRelation("categories", "id", "name"))
self.assetModel.setSort(ROOM, Qt.AscendingOrder)
self.assetModel.setHeaderData(ID, Qt.Horizontal,
QVariant("ID"))
self.assetModel.setHeaderData(NAME, Qt.Horizontal,
QVariant("Name"))
self.assetModel.setHeaderData(CATEGORYID, Qt.Horizontal,
QVariant("Category"))
self.assetModel.setHeaderData(ROOM, Qt.Horizontal,
QVariant("Room"))
self.assetModel.select()
The model is created in much the same way as we saw in the preceding sec-
tion. The ID, NAME, and others are integer column indexes set up earlier in the
assetmanager.pyw file. What’s different from using a QDIalogWidget
The initializer is typical of most delegate subclasses, simply calling the
base class.
def paint(self, painter, option, index):
myoption = QStyleOptionViewItem(option)
if index.column() == ROOM:
myoption.displayAlignment |= Qt.AlignRight|Qt.AlignVCenter
QSqlRelationalDelegate.paint(self, painter, myoption, index)
We have reimplemented the paint() method only to right-align the room
numbers. We do this by changing the QStyleOptionViewItem, and we leave the
painting itself to be done by the base class.
def createEditor(self, parent, option, index):
if index.column() == ROOM:
editor = QLineEdit(parent)
regex = QRegExp(r"(?:0[1-9]|1[0124-9]|2[0-7])"
r"(?:0[1-9]|[1-5][0-9]|6[012])")
validator = QRegExpValidator(regex, par