I have the following code:
class Channel(QObject):
nameChanged = pyqtSignal()
def __init__(self, props, *args, **kwargs):
super().__init__(*args, **kwargs)
self._name = props["name"]
@pyqtProperty("QString", notify=nameChanged)
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
self.nameChanged.emit()
This works perfectly for my current (prototyping) needs. But let's say that props
will have some more keys that I'm also interested in having as pyqtPrierty
.
Is there a way I can generate the setter and the property methods for all the keys in props
?
答案 0 :(得分:0)
解决方案是动态生成的新类型:
from PyQt5.QtCore import pyqtSlot, pyqtProperty, pyqtSignal, QObject
def getter(name):
def wrapped(self):
return self._proxy[name]
return wrapped
def setter(name):
def wrapped(self,v):
self._proxy[name] = v
getattr(self,'proxy_changed').emit()
return wrapped
def dict2props(data):
on_changed = pyqtSignal()
r = {
'_proxy': data,
'proxy_changed': on_changed
}
for k in data:
typ = type(data[k])
setter.__name__ = "set_"+k
getter.__name__ = "get_"+k
r[k] = pyqtProperty(typ, fget=getter(k), fset=setter(k), notify=on_changed)
return r
settings = {'as':12}
settingsproxy = type('Settings', (QObject,), dict2props(settings))()
信号未经测试(将编辑答案,然后完成)。我将通用信号用于所有属性。 但是它也可以生成。
...
def setter(self,v):
self._proxy[k] = v
getattr(self,k+'_changed').emit()
r[k+'_changed'] = pyqtSignal()
r[k] = pyqtProperty(typ,fget=lambda s: self._proxy[k], fset=setter, notify=r[k+'_changed'])
...