Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. passing QDateTime into QML
Qt 6.11 is out! See what's new in the release blog

passing QDateTime into QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.3k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I'm trying to pass a QDateTime object into QML, and I seem to be losing the time zone in the process.

        QDateTime m_dateTimeForTimezone { QDateTime::currentDateTimeUtc().toTimeZone(m_timeZone) };
        Q_PROPERTY(QDateTime dateTimeForTimezone READ dateTimeForTimezone WRITE setDateTimeForTimezone NOTIFY dateTimeForTimezoneChanged FINAL)
    

    In my QML:

    onClicked: {
        dateTime = clock.dateTimeForTimezone
        console.log("PrefsDateTime.qml: dateTime is " + dateTime) // shows the wrong time.
    

    Evidently, this isn't a new issue: this description seems to be the same thing.

    My question: are the proposed solutions in the SO thread really the best way to solve this, or is there a cleaner way to do it?

    Thanks...

    1 Reply Last reply
    0
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #3

      Not really a solution (more of a band-aid), but...I added properties to my clock for the parts of the date/time I needed:

      int m_minutes;
      Q_PROPERTY(int minutes READ minutes WRITE setMinutes NOTIFY minutesChanged FINAL)
      
      int m_hours;
      Q_PROPERTY(int hours READ hours WRITE setHours NOTIFY hoursChanged FINAL)
      

      and referenced them individually:

      property int minutes: clock.minutes
      property int hours: clock.hours
      

      Not a great fix, but it got me past the problem.

      1 Reply Last reply
      0
      • piervalliP Offline
        piervalliP Offline
        piervalli
        wrote on last edited by
        #2

        Nice question I used a string as datetime to avoid the conversion, because I never understand offset on time.

        1 Reply Last reply
        0
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          Not really a solution (more of a band-aid), but...I added properties to my clock for the parts of the date/time I needed:

          int m_minutes;
          Q_PROPERTY(int minutes READ minutes WRITE setMinutes NOTIFY minutesChanged FINAL)
          
          int m_hours;
          Q_PROPERTY(int hours READ hours WRITE setHours NOTIFY hoursChanged FINAL)
          

          and referenced them individually:

          property int minutes: clock.minutes
          property int hours: clock.hours
          

          Not a great fix, but it got me past the problem.

          1 Reply Last reply
          0
          • mzimmersM mzimmers has marked this topic as solved on

          • Login

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