[PyQt] Question about model/view framework

Linos info at linos.es
Wed Feb 27 15:55:42 GMT 2008


IloChab escribió:
> 
> 
> 2008/2/26, Linos <info at linos.es <mailto:info at linos.es>>:
> 
>     IloChab escribió:
> 
>      > I'm using the couple QTableView+QSqlQueryModel.
>      >
>      > I was wondering if it's possible to extend the table with some
>     columns
>      > not contained in the relation but computed on the fly from the
>     application.
>      >
>      > If it's possible I'd like to know how, because I made some tests
>     without
>      > any success.
>      >
>      > Thank you in advance.
>      > ciao
>      > Licia
>      >
>      >
> 
>      >
>     ------------------------------------------------------------------------
>      >
>      > _______________________________________________
>      > PyQt mailing list    PyQt at riverbankcomputing.com
>     <mailto:PyQt at riverbankcomputing.com>
>      > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
>     Hi,
> 
>     if the values are from computed from database columns values you can
>     do in the sql select, if you need to post
>     any other information not generated from sql i think you will have
>     to subclass the model and reimplement the
>     data method.
> 
> 
> Yes, this was what I thought to do, but I wasn't able to let me call on 
> column numbers over the last from my db table.
> So I searched for a method to tell the table header that it was bigger 
> than the table columns number but I didn't find anything useful
> Perhaps I have to create a temporary table just to contain the new 
> columns? But this will be a big waste of db space.
> Or I have to manage the table all by my self, extracting data from the 
> relation and then adding the new calculated columns, but in this way 
> I'll  loose all the model/view features!!
> I guess there must be a better way.
> 
>     Regards,
>     Miguel Angel.
> 
> 
> Ciao
> Licia
> 

Hi,

I think i am not understanding well the problem, with qsqlquerymodel you can do anything like this:

model.setQuery(QString("SELECT INVOICE_NUMBER, CUSTOMER, TOTAL_INVOICE, TAX_INVOICE, TOTAL_INVOICE+TAX_INVOICE 
FROM INVOICE"), dbconnection)

and the "TOTAL_INVOICE+TAX_INVOICE" should be the fifth, depending on the database you are using you could use 
many other functions to string formatting for example inside the select query, you can develop your own 
functions if you any not supported by your database, other way could be create a model where you load in a 
python object the data you retrieve from a database and other information you want to manipulate/show, and 
call it with the model data method, i am using this now and works very well, i have inside the model a list of 
python objects, every line is a object and the class for this objects only have the atributes i retrieve from 
database (i maintain them in QVariant after database retrieve to avoid innecesary type transformations because 
i have to retrieve in this type from the  model data method to tableview), this way you can manipulate that 
you retrieved like you want and use the model/view.


More information about the PyQt mailing list