[PyQt] QAbstractItemModel.setData() and emit

Andreas Pakulat apaku at gmx.de
Thu Jul 19 21:16:56 BST 2007


On 19.07.07 12:54:25, Tom Brown wrote:
>   def index(self, row, column, parent):
>     data = self.queryData[row][column]
>     return self.createIndex(row, column, data)

You don't need the internalPointer data here, after all you index your
data table with the row and column from the model index.

The internalPointer is specifically meant to be used if you need to
transport more information than just row+column to access the data in
data() or setData() or other member functions.

>   def insertRows(self, row, count, parent):
>     self.beginInsertRows(parent, row - 1, row - 1)
>     self.queryData.insert(row, [0, ''])
>     self.endInsertRows()
>     return True
> 
>   def insertRecord(self, value):
>     if self.insertSQL:
>       connection, cursor = getPgCursor(self.dbInfo)
>       try:
>         try:
>           id = getNextVal(cursor, self.sequence)
>           sql = self.insertSQL % (id, value)
>           cursor.execute(sql)
>           connection.commit()
>         finally:
>           connection.close()
>       except:
>         return (0, '')
>       return id, value
>     return (0, '')
> 
>   def setData(self, index, value, role):
>     id, value = self.insertRecord(str(value.toString()))
>     if id:
>       data = index.internalPointer()
>       data[0] = id
>       data[1] = value
>       self.emit(SIGNAL('dataChanged(const QModelIndex &, '
>         'const QModelIndex &)'), index, index)
>       return True
>     return False

This still looks wrong. insertRecord inserts a new row as far as I can
see so you actually need to call begin/endInsertRows and shouldn't
change the value of an existing entry in the table.

Also I wonder why you don't use the existing Qt models for SQL
databases??

Andreas

-- 
A visit to a strange place will bring fresh work.


More information about the PyQt mailing list