Skip to content

FormWidget fails to get a date or datetime value with PySide2 #18958

Closed
@soininen

Description

@soininen

Bug report

Bug summary

matplotlib.backends.qt_editor._formlayout.FormWidget.get() converts values of date and datetime fields into python datetime.date and datetime.datetime objects using toPyDate() and toPyDateTime() methods. These exist only in PyQt5; in PySide2 the corresponding functions are called toPython().

One way to fix this is to patch matplotlib.backends.qt_compat._setup_pyqt5() by adding the following lines under if QT_API == QT_API_PYSIDE2:

    ...
    elif QT_API == QT_API_PYSIDE2:
        from PySide2 import QtCore, QtGui, QtWidgets, __version__
        import shiboken2
        def _isdeleted(obj): return not shiboken2.isValid(obj)
        QtWidgets.QDateTime.toPyDateTime = QtWidgets.QDateTime.toPython  # fix QDatetime
        QtWidgets.QDate.toPyDate = QtWidgets.QDate.toPython # fix QDate
    else:
    ....

Code for reproduction

from datetime import datetime
from PySide2.QtWidgets import QApplication
from matplotlib.backends.qt_editor._formlayout import FormWidget

QApplication()
widget = FormWidget([("Date time", datetime(year=2020, month=11, day=16))])
widget.setup()
values = widget.get()

Actual outcome

Traceback (most recent call last):
  File "formwidget.py", line 8, in <module>
    values = widget.get()
  File "lib\site-packages\matplotlib\backends\qt_editor\_formlayout.py", line 346, in get
    value = field.dateTime().toPyDateTime()
AttributeError: 'PySide2.QtCore.QDateTime' object has no attribute 'toPyDateTime'

Expected outcome

No output, especially no tracebacks.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.0 (the bug seems to be in master too)
  • Matplotlib backend (print(matplotlib.get_backend())): qt5agg
  • Python version: 3.7.6
  • Jupyter version (if applicable):
  • Other libraries: PySide2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions