[PyQt] QSqlQueryModel write subclass

Linos info at linos.es
Sun Oct 21 23:10:37 BST 2007


well my model its working now (i have tested with tableview and datawidgetmapper) but i have a question to the
list about the way i have designed it, now i load with a select the model (original sqlquerymodel) and when
from the view add new rows i use an internal buffer (a list of python objects with the same struct as table),
i have implemented data, setdata, insertrows and removerows this way and it works ok for me, when i update a
sql returned row i use sql directly and relaunch the query to update the view and when i edit a field in the
object list edit the list i launch an datachanged signal to update the view and i have created a save()
function to save all the buffer to database and clear the buffer, but i have any questions to the list, in
qtcentre.org forum a user (that has 7081 posts wow) has post this:

'''
In my opinion you shouldn't subclass the sql query model. It might prove easier to implement a model from
scratch using QAbstractTableModel or even QStandardItemModel and using QSqlQuery and/or QSqlTableModel behind
the scenes. Otherwise you'll have problems with updating the model - after each change to the sql table,
you'll have to use select() to fetch the updated model from the database (or it won't update), rendering the
whole model-view gain practically useless, because you'll be losing selections, current items, etc.
'''

so after think a bit about it i think i can be misusing the model, i could use sqlquery inside the model to
load a list of python objects and use it in the model, i am using this now in the editbuffer, emitting
datachanged works ok with this, i could do the changes to database in background in the model, but i dont know
if the speed would be good with large datasets, any comments about this would be appreciated, thanks.

Linos escribió:
> David thanks for your reply i get this to work yesterday, the problem was that i was not implementing
> rowcount() so the new line was painted in the view but it did not have real index so data() and setdata()
> could not use it, i am today working in a Qsqlquerymodel subclass with a buffer of rows to insert it all in a
> final transaction if the model have dirty property true.
> 
> Best Regards,
> Miguel Angel.
> 
> David Boddie escribió:
>> 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
>>
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list