[PyQt] QSqlTableModel and complex queries

Aaron Digulla digulla at hepe.com
Thu Oct 9 12:56:38 BST 2008


Quoting Mark Summerfield <mark at qtrac.eu>:

> On 2008-10-08, Aaron Digulla wrote:
>> Scott Frankel schrieb:
>> > This seems beyond the scope of
>> > QSqlRelationalTableModel.
>>
>> During my work on UPCScan, I've found that the QSqlRelationalTableModel
>> and the related table view are too buggy and too limited to be useful.
>> In the end, I've ripped all the code out again and wrote my own
>> implementation. Problems that I've encountered:
>>
>> - When you change anything in the model, the view resets itself. You
>> lose the current selection and the current scroll position.
>
> That is intended behaviour. Reset is the model's way of telling
> its associated views that some major data change has occurred and that
> all data shown must be refetched. So if you want to keep selection and
> position info you must handle that yourself.

When I said "change anything in the model", I was referring to  
setData(), insertRows() and removeRows(). So as it is, the  
QSqlQueryModel is only useful for display of static tables.

It's an open bug in Qt 4.4. I found something about this in the Qt  
support forum.

> If you've just changed a one or a few items then emit the dataChanged()
> signal with the relevant QModelIndexes; that shouldn't affect seletions
> etc. and the views will just refetch what has changed.

Please try it. It won't work as expected.

>> - You can't tell the model that you've just added a row to the table.
>
> That's handled in your QAbstractTableModel subclass's insertRows()
> reimplementation, where you call beginInsertRows(), then insert your row
> or rows, then call endInsertRows().

That's not correct. QSqlQueryModel has its own idea what is in the  
table and there is no way to tell it "there is a new row in the table"  
without calling select() (which in turn resets the view).

>> - I have a column with binary data. There is no way to inject a mapper
>> to turn this into something useful for the UI.
>>
>> - I couldn't update the binary data with in setData().
>
> You'd need to implement a custom delegate. A custom delegate you
> control over how data is displayed or over how it is edited or both.

Maybe but because I couldn't get the UI workflow right, I gave up when  
I ran into these issues and I didn't have an obvious solution. I saw  
it in your book but my solution is much more simple and obvious.

Regards,

-- 
Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://www.pdark.de/



More information about the PyQt mailing list