[PyQt] Customized view of a database table

Mattia Borsalino mattia.borsalino at gmail.com
Tue Feb 3 03:56:55 GMT 2009


I'm having trouble performing a task that I thought should be very common,
but that instead appears to be an unusual need.  In short, I have a database
table that has three columns: let's say they are ID, Name and Savings, in
this order. Now, I would like to retrieve the table and for that I can use a
QSqlTableModel, that I can then visualize with a table view. But what if I
want to achieve the following (I'm only interested in the model to be

a) visualize the table with column 1 and 2 swapped, so that the table that I
look at shows ID, Savings, Name?

b) show one more column in the table, which does not exist in the database.
The column contains custom data that should be set by the user through a
dialog for example.

Am I missing something terribly evident here? So far I've tried the
following, without any success:

1. use QSqlRelationalTableModel (I actually have foreign keys), and use the
insertColumn method. That kind of worked, but when I try to fill the column
with setData I can either fill the previous, or the following column.

2. use QProxyModel, with no success whatsoever.

3. use QSqlRelationalTableModel, loop through each element of the model, and
fill a QTableWidget with the data. Then add a column to the QTableWidget and
fill it with the additional data I wanted to display. This of course works,
but it appears rather cumbersome and inefficient. Furthermore, it doesn't
follow the Model/View philosophy at all.

Any help would be much appreciated. Sorry for the verbose email.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090203/14636603/attachment-0001.html

More information about the PyQt mailing list