PDA

View Full Version : pyqt how to get values from the model when I have a QModelIndex



flapjack
18th November 2009, 06:31
I've paraphrased the code so hopefully it makes sense.

I have a QSqlTableModel that populates a QTableView when the user selects a row from the QTableView they are allowed to edit a memo field from the row.

The memo field is a QPlainTextEdit

The following is a snipet from the main app which shows the delegate being maped to qplaintextedit.
self.mapper = QDataWidgetMapper()
self.mapper.setModel(self.recordsModel)
self.mapper.addMapping(self.plainTextEdit, RECORD)

delegate = TextDelegate()
#self.tableView.setItemDelegateForColumn(NAME, delegate)
self.mapper.setItemDelegate(delegate)

QObject.connect(self.tableView.selectionModel(), SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),
self.mapper, SLOT("setCurrentModelIndex(QModelIndex)"))

The following is a snipet of code from the delegate which calls a method in the model
def setModelData(self, editor, model, index):
model.updateMyRecord(index, editor.toPlainText())

the following is the code in the model that creates the record and updates it.
def updateMyRecord(self, index, mytext):
#itest = index.sibling(index.row(), 3)
#idata = itest.data
#sys.stdout.write(str(idata))

#testr = self.record(index.row())
#tests = testr.value(0)
#sys.stdout.write(tests)

record = self.createMyRecord(mytext)

self.setRecord(index.row(), record)

createMyRecord uses mytext to create a complete QSqlRecord and passes it back out. All this works just fine and updates the record corectly. however I'd like to do some aditional processing in another table and need to get at the ID colum of the record. How do I get that out of the QModelIndex? I've commented out a a couple of attempts but nothing seems to work. for example


itest = index.sibling(index.row(), 3)
idata = itest.data
sys.stdout.write(str(idata))

just give me
<built-in method data of QModelIndex object at 0x026CD928> which sounds like some c pointer? but not the actual data. Any ideas? I've tried googling and reading the reference docs but it's not poping out at me. seems .data should work but doesn't with python for some reason.
thanks,
AaronS

flapjack
18th November 2009, 18:06
ha this works to get the value I want. of course the index doesn't hold the data but the models .data method is what I wanted with the index I have.



itest = self.data(index.sibling(index.row(), 0))
#print itest.canConvert(QVariant.String)
idata = itest.toString()
#print idata
sys.stdout.write(idata)