[PyQt] QSqlTableModel and complex queries

Mark Summerfield mark at qtrac.eu
Thu Oct 9 08:33:50 BST 2008


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.

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.

> - 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().

> - 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.

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Rapid GUI Programming with Python and Qt" - ISBN 0132354187



More information about the PyQt mailing list