mottosso / Qt.py

Minimal Python 2 & 3 shim around all Qt bindings - PySide, PySide2, PyQt4 and PyQt5.
MIT License
922 stars 252 forks source link

Handling PyQt4.QtCore.QDate.toPyDate vs PySide2.QtCore.QDate.toPython #281

Open RachelKLowe opened 6 years ago

RachelKLowe commented 6 years ago

Hello, we are using Qt.py to help with our transition from PyQt4 to PySide2 and I've run into some problems with QDate objects. I have a dialog that needs to be able to function in PyQt4 in one environment, and in PySide2 inside of Maya 2018. It is using a QDateEdit, QDateEdit.date() returns a QDate, but in PyQt4 we need to call QDateEdit.date().toPyDate() to get a datetime object back and in PySide2 we need to call QDateEdit.date().toPython().

I tried handling this in QtSiteConfig.py with the following update_compatibility_members method:

def update_compatibility_members(members):
    """This function is called by Qt.py to modify the modules it exposes.

    Arguments:
    members (dict): The members considered by Qt.py
    """
    members['PyQt4']["QtCore.QDate"] = {
        "toPython": "QtCore.QDate.toPyDate",
    }
    members['PyQt4']["QtCore.QDateTime"] = {
        "toPython": "QtCore.QDate.toPyDate",
    }

The code is executing, print statements inside of the method are visible, but when my bindings are PyQt4 QDate objects still do not have a toPython method on them.

Any help to get this working site wide would be greatly appreciated. For now we are just using hasattr to check for toPython on the QDate, and doing QDate.toPython = QDate.toPyDate if it isn't find, but it would be nice to fix it centrally. Thanks!

Kenny

mottosso commented 6 years ago

Thanks @KennethLowe, seems like a simple misaligned member in PySide2.

What I'd suggest is done is to unify them like this.

from Qt import QtCompat
QtCompat.QDate.toPyDate()

Such that this new QDate would represent a compatibility wrapper around the Qt 4 and Qt 5 versions. We can't (shouldn't) add it to the original QDate as it would mean (1) monkey patching and (2) leaving the old one behind and nearby, running the risk of either intermixing them in code or using the wrong one.

Have a look at adding this to the _compatibility_members dictionary, and see whether it produces the results you're looking for. Then a PR would trigger the automatic tests and hopefully solve the issue, then I'd be able to merge.