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

PySide6 QDateTimeEdit.setDateTime() doesn't respect timeSpec() - is it a bug or change from PySide2?



  • Hi,
    below you may find an example of code. It takes Unix timestamp value 1633315080 that represents 2021-10-04 02:38:00 GMT (you may check it at https://www.epochconverter.com/). Then it converts this value into QDateTime object and displays it in QDateTimeEdit widget.
    From print output in console I have:
    Date/Time: 2021-10-04 02:38:00
    Spec: PySide6.QtCore.Qt.TimeSpec.UTC
    that is right, but in GUI window I see 03/10/2021 23:38:00 that is not right.
    If I replace PySide6 by PySide2 I have the same output in console but 2021-10-04 02:38:00 is displayed in GUI.

    I.e. it means for both PySide2/PySide6 I have correct timeSpec (UTC) set for QDateTimeEdit. But when setDateTime() is called PySide2 takes its argument as UTC time (that matches timeSpec) but PySide6 takes its argument as local time (and then, for me, shifts it by 3 hours to UTC).

    But I haven't seen anything about it in Qt5->Qt6 porting quide. So, was there a behavior change of QDateTimeEdit.setDateTime() method or it is a bug? May someone comment?

    Here is the code:

    from datetime import datetime
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import QApplication, QMainWindow, QDateTimeEdit
    
    app = QApplication([])
    window = QMainWindow()
    
    editor = QDateTimeEdit(window)
    editor.setTimeSpec(Qt.UTC)
    editor.setFixedWidth(editor.fontMetrics().horizontalAdvance("00/00/0000 00:00:00") * 1.25)
    editor.setDisplayFormat("dd/MM/yyyy hh:mm:ss")
    
    timestamp = 1633315080  # 2021-10-04 02:38:00 GMT
    date_time = datetime.utcfromtimestamp(timestamp)
    print(f"Date/Time: {date_time}")
    
    editor.setDateTime(date_time)
    print(f"Spec: {editor.timeSpec()}")
    window.show()
    app.exec()
    


  • Ok, in web-manual of QDateTimeEdit.dateTime property I see:
    for Qt6:
    When setting this property, the new QDateTime is converted to the timespec of the QDateTimeEdit, which thus remains unchanged.
    for Qt5:
    When setting this property the timespec of the QDateTimeEdit remains the same and the timespec of the new QDateTime is ignored.

    So, indeed, behavior was changed...
    But... Is there anywhere a list of such changes? It's a bit annoying to find them occasionally one by one...


  • Lifetime Qt Champion

    Hi,

    There's the list of changes. If that one is not on there, I would say that this is a documentation bug.



  • @SGaist, QDateTimeEdit is from Qt Widgets so I assume this change should be described on this page but I don't see it there. I.e. it appears the list of changes is incomplete :(


  • Lifetime Qt Champion

    You should open a bug report for the documentation about that behaviour change.




Log in to reply