This recipe will find all attributes created as meta-object properties in Qt and will create a Python property of the same name for each of them. This is a quick way of providing some of the functionality suggested by PSEP 102, which I sincerely hope will be accepted, as it will make PySide considerably more pythonic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | def iterProperties(cls):
"""
Iterates through the names of all the properties on a PyQt class.
"""
meta = cls.staticMetaObject
for i in range(meta.propertyCount()):
yield meta.property(i).name()
def useProperties(cls):
"""
Adds Python properties for each Qt property in a class.
"""
def getter(name):
def get(self):
return self.property(name)
return get
def setter(name):
def set(self, value):
return self.setProperty(name, value)
return set
for name in iterProperties(cls):
setattr(cls, name, property(getter(name), setter(name)))
return cls
# use in Python 3
@useProperties
class Widget(QtGui.QWidget):
pass
# or simply
useProperties(QtGui.QWidget)
# then
w = QtGui.QWidget()
w.font = QtGui.QFont('Droid Sans Mono')
|
Note that this will not replace any getters/setters for attributes that are not declared as properties, such as QWidget.setParent.