Results 1 to 2 of 2

Thread: pyqt how to get values from the model when I have a QModelIndex

  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Platforms
    Windows

    Default pyqt how to get values from the model when I have a QModelIndex

    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

  2. #2
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: pyqt how to get values from the model when I have a QModelIndex

    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.

    Qt Code:
    1. itest = self.data(index.sibling(index.row(), 0))
    2. #print itest.canConvert(QVariant.String)
    3. idata = itest.toString()
    4. #print idata
    5. sys.stdout.write(idata)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Passing values to custom 'slot' functions (pyqt)
    By Richie in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 07:05
  2. Replies: 1
    Last Post: 3rd June 2009, 19:08
  3. Writing a Tree model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2009, 11:22
  4. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 17:31
  5. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.