[PyQt] Type map for python to Qt?

Phil Thompson phil at riverbankcomputing.com
Thu Feb 2 16:18:07 GMT 2017


On 2 Feb 2017, at 12:39 pm, Russell Warren <russ at perspexis.com> wrote:
> 
> Is there a table somewhere that indicates how PyQt handles mapping between python types to Qt/C++ types? I can't find one.
> 
> Introspecting a random selection of some python types is extremely informative:
> 
> >>> from PyQt5.QtCore import QVariant
> >>> import datetime
> >>> dt = datetime.datetime(2017,1,1)
> >>> for x in ("a", b"b", 1, 1.1, [], [1], (1,), {}, {'a': 1}, {1:2}, None, True, set(), dt): 
> ...   print("%-20r : %s" % (x, QVariant(x).typeName()))
> ... 
> 'a'                  : QString
> b'b'                 : PyQt_PyObject
> 1                    : int
> 1.1                  : double
> []                   : QVariantList
> [1]                  : QVariantList
> (1,)                 : PyQt_PyObject
> {}                   : QVariantMap
> {'a': 1}             : QVariantMap
> {1: 2}               : PyQt_PyObject
> None                 : None
> True                 : bool
> set()                : PyQt_PyObject
> datetime.datetime(2017, 1, 1, 0, 0) : PyQt_PyObject
> 
> Does that table exist anywhere?

Not to my knowledge.

> Also of interest (not a bug report or complaint) is the apparent lack of automatic type conversion for bytes, tuples, and datetimes, as well as the recognition of non-string dict keys.

Conversions are implemented only if there is a direct equivalent, everything else gets converted to a PyQt_PyObject. You could perhaps make the case for the date/time types.

Phil


More information about the PyQt mailing list