Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QtCore.QVariant for PySide #5

Closed
davidlatwe opened this issue Feb 1, 2020 · 4 comments
Closed

QtCore.QVariant for PySide #5

davidlatwe opened this issue Feb 1, 2020 · 4 comments

Comments

@davidlatwe
Copy link

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
Copy link
Author

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
Copy link
Owner

mottosso commented Feb 1, 2020

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
Copy link
Author

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

@hannesdelbeke
Copy link

believe this issue can be closed?

@mottosso mottosso closed this as completed Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants