1

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?

ZF007
  • 3,708
  • 8
  • 29
  • 48
  • There is rarely any need to use `QVariant` in PyQt5. Normal Python objects can be passed directly to any Qt API that takes a `QVariant`. A null `QVariant` can be replaced by `None`. – ekhumoro Dec 20 '17 at 17:25
  • I thought it was a particular solution to address a class object issue I experienced but instead I've added the method inside a socket_server not an elegant solution but does the job for now). So I've taken it out again. I'll post that issue later this week with a ref to it here. (its more or less your example I've linked that helped me out trying the QVarant which at first glance looked solving the issue, but now I'm wirting down the issue based on your program structure tree; not jet ready to post it). – ZF007 Dec 20 '17 at 19:03

1 Answers1

2

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().

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • I know it works... but I need to get the data at that object and not the location. Its all about the method `toPython()` that has ben changed into `value()` and humanly unreadable. – ZF007 Dec 20 '17 at 10:53
  • Lol... I came to the same conclusion.. refreshed page to update... >thumbs up< Solved! – ZF007 Dec 20 '17 at 11:16