mottosso / Qt5.py

Minimal Python 2 & 3 shim around PySide2 and PyQt5
MIT License
33 stars 5 forks source link

QtCore.QVariant for PySide #5

Closed davidlatwe closed 2 years ago

davidlatwe commented 4 years ago

QtCore.QVariant was missing in PySide, but the document mentioned that one could just use "QVariant" like:

signal = QtCore.Signal("QVariant")

This works for Python built-in types, but looks like not all custom types will be handled properly.

import sys
from PySide2 import QtWidgets, QtCore

QtCore.QVariant = "QVariant"

class Sheet(list):
    pass

class Thing(object):
    pass

class Application(QtWidgets.QApplication):

    variant = QtCore.Signal(QtCore.QVariant)

    def __init__(self):
        super(Application, self).__init__(sys.argv)

        window = QtWidgets.QWidget()
        button = QtWidgets.QPushButton("Emit")

        layout = QtWidgets.QVBoxLayout(window)
        layout.addWidget(button)

        button.clicked.connect(self.emit_variant)
        self.variant.connect(self.process)
        self.window = window

        window.show()

    def emit_variant(self):
        self.variant.emit(Sheet(["a", "b"]))
        self.variant.emit(Thing())

    def process(self, variant):
        print(type(variant), variant)

if __name__ == "__main__":
    app = Application()
    app.exec_()

Hit the Emit button and the result will be:

<class 'list'> ['a', 'b']
<class '__main__.Thing'> <__main__.Thing object at 0x000002204CEA1240>

Notice that although the instance of Thing stays the same, but the instance of Sheet has became just a list.

But if you change "QVariant" to object

QtCore.QVariant = object

Then:

<class '__main__.Sheet'> ['a', 'b']
<class '__main__.Thing'> <__main__.Thing object at 0x000001E0A0DC1240>

They both get preserved.

So maybe we should use object instead of "QVariant" to fill up this gap ? And, anyone knows why it works in this way in PySide ?

davidlatwe commented 4 years ago

So maybe we should use object instead of "QVariant" to fill up this gap ?

In fact, just found that QtCore.QVariant = object will not work for string type in QtCore.Property.

mottosso commented 4 years ago

I'm unsure about this. Qt5.py, like Qt.py, should use the PySide2 bindings as target interface. And PySide2 doesn't have QVariant. So, ideally, Qt5.py shouldn't have QVariant either.

If we do add it, then it's not clear what else to add that isn't already in PySide2. :S

Would it be an option for you to stick with "QVariant", as recommended by the PySide2 authors?

davidlatwe commented 4 years ago

Yes, you are right, shouldn't start adding things that wasn't there. And yes, stick with "QVariant" is the best option. ☺️

hannesdelbeke commented 2 years ago

believe this issue can be closed?