[PyQt] QAbstractItemModel.setData() and emit

Tom Brown brown at esteem.com
Thu Jul 19 21:53:30 BST 2007


On Thursday 19 July 2007 13:16, Andreas Pakulat wrote:
> 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??

I started out using the Qt models for four comboxes. This was during 
development on my linux box. That worked very well and I was able to easily 
subclass QSQLQueryModel. Then I went to test it out in Windows and it 
wouldn't run because it needed PostgreSQL drivers. That means I'd have to 
install PostgreSQL on all the target client machines in production. I thought 
I could avoid that by creating my own model. Now it is looking like 
installing PostgreSQL may be an easier solution.

I ran the ModelTest with my model and I had to kill it after 13 minutes. It 
looks like it got stuck on createIndex() for some reason.

I guess I'm confused about how the model is supposed to work. insertRecord() 
inserts a new record into the database, but not a new row into the 
self.queryData that is used to populate the combobox. insertRows() does 
insert a new (empty) row into self.queryData. Then setData() populates the 
empty row. Well, that's what I think should happen.

Thanks,
Tom


More information about the PyQt mailing list