QDateTimeEdit - setDateTime() doesn't work [Value from Database]
-
wrote on 8 Jun 2011, 06:26 last edited by
Hello,
I have a SQLite Database with several DateTime Data types. The Problem is that when I try to set the DateTimeEdit with following Statement, nothing will happen - the DateTimeEdit keeps it's old DateTime.
@ui_dialog.dteEdit_begin->setDateTime(edt_query.value(1).toDateTime());@The Statement for writing the values into the database is following:
@ui_dialog.dteEdit_begin->dateTime();@How can I solve this problem as there is no way to clear the DateTimeEdit?
-
wrote on 8 Jun 2011, 09:21 last edited by
Due to debugging I could figure out that the query result @edt_query.value(1).toDateTime()@ is empty. But when I call @edt_query.value(1).toString@ I get the Date and even in correct format.
But QDateTimeEdit doesn't accept strings. How can I convert a string in QDateTime?
-
wrote on 8 Jun 2011, 10:42 last edited by
@ui_dialog.dteEdit_begin->setDateTime(QDateTime::fromString(edt_query.value(1).toString, "yyyy-MM-dd"));@
-
wrote on 8 Jun 2011, 18:28 last edited by
SQLite does not have a dedicated DateTime type. It stores its values as text, real or integer. Thus you will have to convert this to a QDateTime using the appropriate conversion methods or constructors.
-
wrote on 9 Jun 2011, 07:14 last edited by
I have this DateTime format:
@Do 9. Jun 09:04:22 2011@What is the appropriate format for the code?
I tried:
@ui_dialog.dteEdit_begin->setDateTime(QDateTime::fromString(edt_query.value(1).toString(),
"ddd-d-MMM-hh-mm-ss-yyyy"));@But the DateTimeEdit still keeps its old DateTime?!
-
wrote on 9 Jun 2011, 07:43 last edited by
Insert your's separators instead of "-". For example:
DB's DateTime to string: 09:04:22 09-06-2011
Your shoud insert: "hh:mm:ss dd-MM-yyyy" -
wrote on 9 Jun 2011, 08:03 last edited by
But in my particular case Date and Time are mixed and not together. Is the approach the same? Something like this? @ddd-d-MMM hh:mm:s yyyy@
Do 9. Jun 09:04:22 2011
-
wrote on 9 Jun 2011, 08:21 last edited by
Your date string doesn't have dashes ( - ) in it. Why do you include them in your format string?
On the other hand, can't you coax SQLite into returning your date in a standard format instead? Hard-coding this format (seems it might be dependent on a system setting) sounds like a brittle solution.
-
wrote on 9 Jun 2011, 08:25 last edited by
If I remember correctly SQLite supports "date and time functions":http://www.sqlite.org/lang_datefunc.html that use a subset of the IS0-8601 date and time formats (although they are stored as plain text as Volker stated) which are supported by Qt too.
If you stick to
@
QDateTime::toString(Qt::ISODate) // when storing to the database
QDateTime::fromString(QString(), Qt::ISODate) // when retrieving from the database
@
you should be on the safe side.However, if you are tied to non-conform date/time formats QDateTime::fromString is the way you do it, using
@
QDateTime::fromString(QString(), "ddd d. MMM hh:mm:ss yyyy").
@
in your example. Just replace the date/time values with the placeholders listed in the "documentation":http://doc.qt.nokia.com/latest/qdatetime.html#fromString-2 and leave everything else as it is (whitespaces, leading and trailing characters). -
wrote on 9 Jun 2011, 08:38 last edited by
bq. Your date string doesn’t have dashes ( – ) in it. Why do you include them in your format string?
Okay, now I begin to understand! Thank you!
-
wrote on 9 Jun 2011, 11:36 last edited by
I suggest storing the dates in SQLite in either ISO format or as integer (using seconds since epoch) and convert that back with the respective methods of QDateTime. Don't use local date formats as those vary from country to country and are hard to debug anyways. Storing the weekday in the database is redundant anyways.
1/11