1

I am tired of writing the same code for declaring Properties for PySide2:

class BackEnd(QObject):

    my_prop_updated = Signal()

    @Property(bool)
    def my_prop(self):
        print('getter invoked'))
        return self._my_prop

    @my_prop.setter
    def set_my_prop(self, value):
        print('setter invoked')
        self._my_prop = value
        self.my_prop_updated.emit()

    def __init__(self):
        super().__init__()
        self._my_prop = None

Is there any way to make use of python descriptors with Properties? Something like so:

class QTPropBool(Property):
    my_prop_updated = Signal()

    def my_getter(self, obj):
        print('getter invoked')
        return self._my_prop

    def my_setter(self, obj, value):
        print('setter invoked')
        self._my_prop = value
        self.my_prop_updated.emit()

    def __init__(self):
        super().__init__(bool, self.my_getter, self.my_setter, notify=self.my_prop_updated)


class BackEnd(QObject):
    my_prop = QTPropBool()

It gives me an error:

AttributeError: 'PySide2.QtCore.Signal' object has no attribute 'emit'

Probably because it's Property class, not QObject and I am not invoking QObject's constructor. Is there any way to avoid code duplication?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Artem
  • 107
  • 1
  • 6
  • Why are you writing so many properties? What's the context? – ekhumoro Jul 25 '18 at 15:28
  • I have many text fields in my QML and they are set/updated through Python backend which talks to hardware – Artem Jul 25 '18 at 15:31
  • @Artem mark as a duplicate your question because the code is similar, except for the PyQt5 to PySide conversions, if you can not do it the translation is in the following link: https://gist.github.com/eyllanesc/21e1739e25daffb98e4a7d1fad826096 – eyllanesc Jul 25 '18 at 15:58

0 Answers0