Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDateTimeEdit - setDateTime() doesn't work [Value from Database]
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 10.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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"

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        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 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved