Created
October 29, 2020 11:43
-
-
Save ales-erjavec/75d60adccd9e40dc19208ab047050260 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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