On my little PyQt email program I wanted to be able to generate a list of who would get the emails. I wanted to see this before I actually sent them. I wanted a name and an email to go with it. This means getting the data into a model and then having the view widget load the model. It took me forever to figure out how to do this, as I’m a bit slow. But now that it is done, I’d better document it a bit because I’ll forget and need to do it again later.
Here’s what actually building the model and loading it looks like:
def mpreview(self):
query = 'Select Greeting, User2 from Contact where len(User2) > 3 order by Greeting'
dbase=r'C:\Documents and Settings\JR Peck\My Documents\mpd_stuff\linkedtnt.mdb'
newdata = mpddata.MpdData(query, dbase)
total = len(newdata.rows)
row=0
model = QtGui.QStandardItemModel(total,2)
model.setHorizontalHeaderItem(0,QtGui.QStandardItem('Greeting'))
model.setHorizontalHeaderItem(1,QtGui.QStandardItem('Email'))
for record in newdata.rows:
model.setItem(row,0,QtGui.QStandardItem(record.Greeting))
model.setItem(row,1,QtGui.QStandardItem(record.User2))
row = row + 1
self.ui.tblRecip.setModel(model)
The reference for QTableView was helpful. It just took me a while to understand what it would take to build a QStandardItemModel. Once I got that figured out, then it was easy.

