playuing with properties
This commit is contained in:
parent
0f2d5f3f37
commit
a15b4f56d4
72
qt5.py
72
qt5.py
@ -28,34 +28,50 @@ else:
|
|||||||
QtCore.Signal = QtCore.pyqtSignal
|
QtCore.Signal = QtCore.pyqtSignal
|
||||||
|
|
||||||
|
|
||||||
def propertize(class_):
|
class Propertize(type):
|
||||||
class_dict = class_.__dict__.copy()
|
def __new__(cls, name, bases, dct):
|
||||||
for name, attr in class_dict.items():
|
return type.__new__(
|
||||||
if isinstance(attr, type):
|
cls,
|
||||||
propertize(attr)
|
name[1:],
|
||||||
else:
|
tuple(Propertize.this_class(base) for base in bases),
|
||||||
try:
|
{
|
||||||
setattr(class_, "get" + name.capitalize(), copy(attr))
|
attr_name: (
|
||||||
setattr(
|
property(attr, dct["set" + attr_name.capitalize()])
|
||||||
class_,
|
if "set" + attr_name.capitalize() in dct
|
||||||
name,
|
else (
|
||||||
property(
|
Propertize.this_class(attr)
|
||||||
getattr(class_, "get" + name.capitalize()),
|
if isinstance(attr, type)
|
||||||
getattr(class_, "set" + name.capitalize())
|
else attr
|
||||||
)
|
)
|
||||||
)
|
) for attr_name, attr in dct.items()
|
||||||
print(getattr(class_, "get" + name.capitalize()))
|
}
|
||||||
except AttributeError:
|
)
|
||||||
pass
|
|
||||||
|
@staticmethod
|
||||||
|
def this_class(cls):
|
||||||
"""for module in QtWidgets, QtCore, QtGui, QtMultimedia:
|
return Propertize(cls.__name__, cls.__bases__, cls.__dict__)
|
||||||
for class_ in module.__dict__.values():
|
|
||||||
if isinstance(class_, type):
|
|
||||||
propertize(class_)"""
|
|
||||||
|
|
||||||
propertize(QtCore.QPoint)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
p=QtCore.QPoint(1,1)
|
class QParent:
|
||||||
print(p.x)
|
def __init__(self):
|
||||||
|
self._b = 0
|
||||||
|
def b(self):
|
||||||
|
return self._b
|
||||||
|
def setB(self, val):
|
||||||
|
self._b = val
|
||||||
|
|
||||||
|
|
||||||
|
class QTest(QParent, metaclass=Propertize):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._a = 0
|
||||||
|
def a(self):
|
||||||
|
return self._a
|
||||||
|
def setA(self, val):
|
||||||
|
self._a = val
|
||||||
|
|
||||||
|
|
||||||
|
Point = Propertize.this_class(QtCore.QPoint)()
|
||||||
|
p = Point(1, 2)
|
||||||
|
print(p.x, p.y)
|
Loading…
x
Reference in New Issue
Block a user