pythonpyqt4pyqt5qvariant

PyQt5 - QVariant.value() returns object instead of python value (pyqt4 .toPyObject())


Based on the SO example here and the below output:

from PyQt4.QtCore import QVariant
data = {'key1': 123, 'key2': 456}
v    = QVariant((data,))
v.toPyObject()[0]
>>> {'key2': 456, 'key1': 123}`

I try to accomplish this for pyqt5 but with partial success.

import sys
from PyQt5 import QtCore
from PyQt5.QtCore import QVariant

data = {'key1': 123, 'key2': 456}
v    = QtCore.QVariant((data,))
v.value()[0]

print v

The output is:

<PyQt5.QtCore.QVariant object at 0x0000000006778748>

and not the expected:

{'key1': 123, 'key2': 456} or {'key2': 456, 'key1': 123}

As the implementation of QVariant changed toPyObject() was removed and received value() instead in pyqt5. Any help on how to get the value instead of object location?


Solution

  • It's worked perfectly. v is and Object of QVariant. So when you print v it will display

    <PyQt5.QtCore.QVariant object at 0x0000000006778748>
    

    For each kind of objects it's have it's own methods to call. Access the values by calling the specific methods.

    [Edit] Working

    In [1]: import sys
       ...: from PyQt5 import QtCore
       ...: from PyQt5.QtCore import QVariant
       ...: 
    
    In [2]: data = {'key1': 123, 'key2': 456}
    
    In [3]: v = QtCore.QVariant((data,))
    
    In [4]: v.value()
    Out[4]: ({'key1': 123, 'key2': 456},)
    
    In [5]: print v
    <PyQt5.QtCore.QVariant object at 0x7f6d4e065cf8>
    
    In [6]: print v.value()
    ({'key2': 456, 'key1': 123},)
    

    You printed v instead of v.value().