[Qt-interest] display one database column as two in QTableView

Israel Brewster israel at frontierflying.com
Mon Mar 8 21:54:25 CET 2010


On Mar 8, 2010, at 11:28 AM, André Somers wrote:

> Alternatively, you could create a proxy model that splits the date column
> into two separate columns. This would also work on databases that don't
> support editable queries.

I was thinking about something like that, but hadn't quite figured it out yet. If I'm thinking right, I would then need to override the data() and setData() functions to translate between the real model and proxy, and the columnCount function to return one additional column. Am I missing anything there? I'll give it a try here in a bit to see what happens. Thanks.

> 
> André
> 
> 
> -----Oorspronkelijk bericht-----
> Van: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] Namens Mark Brand
> Verzonden: maandag 8 maart 2010 20:32
> Aan: qt-interest at trolltech.com
> Onderwerp: Re: [Qt-interest] display one database column as two in
> QTableView
> 
> 
>> I have a database which contains a timestamp column. For editing
>> purposes, I want to display this as separate time and date columns. I
>> am using a QSqlTableModel, displayed with a QSqlTableView. How can I
>> make this happen?
>> 
>> My first thought was to subclass QSqlTableModel to expose the setQuery
>> function. I then passed a QSqlQuery like "SELECT
>> date(timestamp_column), time(timestamp_column) FROM table". This
>> worked to display the timestamp column as separate date and time
>> columns. However, when I edited the model and tried to save the
>> changes back to the database, I got a "syntax error around SET".
>> 
>> This might make sense, considering that the model now has two columns
>> whereas the database only has one, so I figured I'd simply override
>> the function that actually writes the data back to the database to
>> manually combine the date and time into a timestamp before submitting
>> the change to the database. It appears to me that the function I need
>> to override is updateRowInTable, but when I tried that it was
>> (apparently at least) never called. Is that the right function to be
>> using?
>> 
>> How can I display a timestamp column as separate date and time
>> columns, and keep it editable? Thanks.
>> 
> 
> I would make it easier on yourself.  One possibility is to use the
> QDateTimeEdit to edit the timestamp as a single column.
> 
> If you want to split it into 2 columns, I would recommend doing this in
> your database with a view (in the database sense, not the model/view
> sense) and a trigger to make the view updatable. If you do this right,
> you can treat the view like a plain old table in QSqlTableModel. There
> is one complication: You will almost certainly have to set the primary
> key in QSqlTableModel yourself since it won't be detected.
> 
> You're halfway there already:
> 
> CREATE VIEW my_view AS
>    SELECT date(timestamp_column) d, time(timestamp_column) t
>    FROM table
> 
> -Mark
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-----------------------------------------------
Israel Brewster
Computer Support Technician II
Era Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Israel Brewster.vcf
Type: text/directory
Size: 417 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100308/1130c5d2/attachment.bin 
-------------- next part --------------



More information about the Qt-interest-old mailing list