Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDateTimeEdit - setDateTime() doesn't work [Value from Database]



  • 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?



  • 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?



  • @ui_dialog.dteEdit_begin->setDateTime(QDateTime::fromString(edt_query.value(1).toString, "yyyy-MM-dd"));@



  • 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.



  • 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?!



  • 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"



  • 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



  • 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.



  • 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).



  • 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!



  • 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.


Log in to reply