[PyQt] QSqlQueryModel write subclass

David Boddie david at boddie.org.uk
Sat Oct 20 01:56:29 BST 2007


On Wednesday 17 October 2007 12:14:52 +0200, Linos wrote:

> I am goin crazy with this problem, i have tested with qsqltablemodel
> subclassing and it works ok if a dont write the insertRows method in the
> subclass:
>
> (model)
> class SkudaSqlTableModel(QSqlTableModel):
>     def __init__(self, dbcursor=None):
>         super(SkudaSqlTableModel, self).__init__()
>
>
> (QDialog)
> def addRecord(self):
>         row = self.querymodel.rowCount()
>         self.querymodel.insertRow(row)
>
>
> this works ok and gives me an new row with an '*' in the vertical header
> and i can edit the line but if i do this:
>
> (model)
> class SkudaSqlTableModel(QSqlTableModel):
>     def __init__(self, dbcursor=None):
>         super(SkudaSqlTableModel, self).__init__()
>
>     def insertRows(self, position, rows=1 , index=QModelIndex()):
>         self.beginInsertRows(QModelIndex(), position, position + rows - 1)
>         self.endInsertRows()
>         return True

Normally, when you reimplement insertRows() you would do something between
calling beginInsertRows() and endInsertRows(). Not being familiar with the
internals of QSqlTableModel, I'm not sure what should be done.

> (QDialog)
> def addRecord(self):
>         row = self.querymodel.rowCount()
>         self.querymodel.insertRow(row)
>
> i get a new line with the next number in the vertical header (not *) and i
> cant edit the line. So i suppose i have any problem in my insertRows method
> but i have read the QSqlTableModel source code insertRows method, i paste
> here the strategy OnRowchange because is what i am using.

OK, so do you basically want to fine-tune the behaviour of the table model
or are you trying to achieve something else by subclassing QSqlTableModel?

[...]

> I cant find any other action to implement in my method apart from
> beginInsertRows and endInsertRows, i am not using the insertIndex number (i
> use rowCount()), not clearEditBuffer() because i have no buffer to clear in
> this test and primeInsert is a signal no connected to anything so i dont
> emit it, anyway i have tried using exactly the same (all of them) in my
> method with exactly the same problem, can anyone please open my closed mind
> with this annoying problem?

I suppose that the lines

  d->insertIndex = row;
  d->clearEditBuffer();

may have some side effects, but it's difficult to see what those could be
without more detailed study. Plus, there's nothing you can do to access
the internals in such a way.

> i will use qsqltablemodel subclass with 
> setQuery (inherited from QSqlQuery) to do joins and other stuff if i cant
> fix this (but this is not recommended in documentation).

I imagine that using setQuery() might interfere with the normal operation
of the model if you're not careful.

David




More information about the PyQt mailing list