[Qt-interest] display one database column as two in QTableView
André Somers
andre at familiesomers.nl
Mon Mar 8 21:28:25 CET 2010
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.
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
More information about the Qt-interest-old
mailing list