Skip to content

Instantly share code, notes, and snippets.

@ales-erjavec
Created October 29, 2020 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ales-erjavec/75d60adccd9e40dc19208ab047050260 to your computer and use it in GitHub Desktop.
Save ales-erjavec/75d60adccd9e40dc19208ab047050260 to your computer and use it in GitHub Desktop.
from PyQt5.QtCore import QObject, pyqtProperty
class Q_Property(pyqtProperty):
"""
A descriptor that more closely resembles the Q_PROPERTY macro.
Encourage a coding style which is consistent with Qt.
Example:
>>> class Foo(QObject):
... _bar = 0
... def bar(self):
... return self._bar
... def setBar(self, bar):
... self._bar = bar
... bar = Q_Property(int, fget=bar, fset=setBar)
>>> obj = Foo()
>>> obj.setBar(2)
>>> obj.bar()
2
>>> obj.setProperty("bar", 42)
True
>>> obj.property("bar")
42
>>> obj.bar()
42
"""
def __get__(self, obj, cls=None):
# Simply return the contained (bound) fget method
return self.fget.__get__(obj, cls)
def __set__(self, obj, value):
raise TypeError
# these cannot work with this style of use
def setter(self, f):
raise RuntimeError
def getter(self, f):
raise RuntimeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment