<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I am playing with dynamically adding signals, slots, and properties at class-declaration time using locals(). The following code works with pyqtSignal and pyqtProperty, but not pyqtSlot? It looks like pyqtProperty can read the attributes from fset and fget, but pyqtSlot doesn’t read the attribute from name?<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(45, 150, 30); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #2d961e" class="">PropertySheet</span><span style="font-variant-ligatures: no-common-ligatures" class="">(QObject):</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(242, 242, 242); min-height: 17px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(45, 150, 30); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(45, 150, 30); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">@staticmethod</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">def</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c6c1d" class="">_setup_pyqtProperties</span><span style="font-variant-ligatures: no-common-ligatures" class="">(classAttrs, fields):</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">def</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c6c1d" class="">closure</span><span style="font-variant-ligatures: no-common-ligatures" class="">(attr, kind):</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">qsignal</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = pyqtSignal()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(242, 242, 242); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">getterName</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""> = attr</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">setterName</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #9d206f" class="">'set'</span><span style="font-variant-ligatures: no-common-ligatures" class=""> + attr[0].upper() + attr[1:]</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">getter</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">lambda</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.get(attr)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">setter</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">lambda</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #696795" class="">set</span><span style="font-variant-ligatures: no-common-ligatures" class="">(attr, x)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">qproperty</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = pyqtProperty(kind,</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> fget=getter,</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> fset=setter,</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> notify=qsignal)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""><span class="Apple-tab-span" style="white-space:pre"> </span></span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">qslot</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = pyqtSlot(kind, name=setterName)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""><span class="Apple-tab-span" style="white-space:pre"> </span></span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">ret</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = {}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">ret</span><span style="font-variant-ligatures: no-common-ligatures" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #9d206f" class="">'%sChanged'</span><span style="font-variant-ligatures: no-common-ligatures" class=""> % attr] = qsignal</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">ret</span><span style="font-variant-ligatures: no-common-ligatures" class="">[getterName] = qproperty</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">ret</span><span style="font-variant-ligatures: no-common-ligatures" class="">[setterName] = qslot</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ret</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> k, (attr, kind, label) </span><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #696795" class="">enumerate</span><span style="font-variant-ligatures: no-common-ligatures" class="">(fields):</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #f2f2f2" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">propAttrs</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = closure(attr, kind)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> classAttrs.update(propAttrs)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(242, 242, 242); min-height: 17px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c200ff" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #2d961e" class="">QmlPersonProperties</span><span style="font-variant-ligatures: no-common-ligatures" class="">(PropertySheet):</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(242, 242, 242); min-height: 17px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(157, 32, 111);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c1651c" class="">FIELDS</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""> = [(</span><span style="font-variant-ligatures: no-common-ligatures" class="">'firstName'</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #696795" class="">str</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">'First Name'</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">),</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(157, 32, 111);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures" class="">'middleName'</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #696795" class="">str</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">'Middle Name'</span><span style="font-variant-ligatures: no-common-ligatures; color: #dadada" class="">),</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> ]</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> PropertySheet._setup_pyqtProperties(</span><span style="font-variant-ligatures: no-common-ligatures; color: #696795" class="">locals</span><span style="font-variant-ligatures: no-common-ligatures" class="">(), FIELDS)</span></div></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(218, 218, 218); background-color: rgba(0, 0, 0, 0.85098);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div><br class=""><blockquote type="cite" class=""><div class="">On May 27, 2019, at 11:29 AM, Kyle Altendorf <<a href="mailto:sda@fstab.net" class="">sda@fstab.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">When do you figure out what the properties should be and when do you need to create the class? You can dynamically create classes with three-arg type() calls.<br class=""><br class="">I went another approach for my needs with signals. A simplified explanation of the idea is that instead of adding signals dynamical to a class you can instead add other QObject instances with signals on them as attributes of your primary class. I wrapped this up with a descriptor which I can use pretty much as a drop in replacement for pyqtSignal but without having to inherit from QObject. Not sure if that approach ends up helpful here though. Of course there is overhead associated with extra objects etc but... sometimes that doesn't matter.<br class=""><br class=""><a href="https://github.com/altendky/stlib/blob/f779e9c4d5ca4015eafe946b3281a9dcdd7a9fd9/epyqlib/utils/qt.py#L1172-L1210" class="">https://github.com/altendky/stlib/blob/f779e9c4d5ca4015eafe946b3281a9dcdd7a9fd9/epyqlib/utils/qt.py#L1172-L1210</a><br class=""><br class="">Cheers,<br class="">-kyle<br class=""><br class="">On May 27, 2019 2:38:04 PM EDT, Patrick Stinson <patrickkidd@gmail.com> wrote:<br class=""><blockquote type="cite" class="">So they are. I wonder if this can be done with attached properties.<br class=""><br class=""><blockquote type="cite" class="">On May 27, 2019, at 9:59 AM, Phil Thompson<br class=""></blockquote><phil@riverbankcomputing.com> wrote:<br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class="">On 27/05/2019 17:41, Patrick Stinson wrote:<br class="">Ok, that answers my question. I will figure out a way to add the<br class="">properties to the class the first time they are added to one of the<br class="">instances. They are always the same, after all.<br class=""></blockquote><br class="">Properties (like signals) are part of the class *definition* (as far<br class=""></blockquote>as Qt is concerned). You can't add them dynamically.<br class=""><blockquote type="cite" class=""><br class="">Phil<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On May 27, 2019, at 9:11 AM, Phil Thompson<br class=""></blockquote></blockquote></blockquote></blockquote><phil@riverbankcomputing.com> wrote:<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On 27/05/2019 16:33, Patrick Stinson wrote:<br class="">I have a custom object property system that adds properties to<br class=""></blockquote></blockquote></blockquote></blockquote>QObject<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">instances and am trying to expose those [dynamic] properties to<br class=""></blockquote></blockquote></blockquote></blockquote>qml.<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">Is it possible to add a qt property to a QObject instance, as<br class=""></blockquote></blockquote></blockquote></blockquote>opposed<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">to adding it using pyqtProperty as a decorator in the class<br class="">declaration?<br class="">The PyQt5 docs say that you can use pyqtProperty in the same way<br class=""></blockquote></blockquote></blockquote></blockquote>as<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">the python property() function apart from the decorator, but I<br class=""></blockquote></blockquote></blockquote></blockquote>haven’t<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">had much success with this:<br class="">def test_property():<br class=""> class A(QObject):<br class=""> def __init__(self):<br class=""> self._mine = 12<br class=""> self.mine = pyqtProperty(int, self.get_mine,<br class=""></blockquote></blockquote></blockquote></blockquote>self.set_mine)<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""> def get_mine(self):<br class=""> return self._mine<br class=""> def set_mine(self, x):<br class=""> self._mine = x<br class=""> a = A()<br class=""> print(a.mine)<br class=""> print(a.mine())<br class="">turin:pkdiagram patrick$ python test.py<br class=""><PyQt5.QtCore.pyqtProperty object at 0x114ea1840><br class="">Traceback (most recent call last):<br class="">File "test.py", line 743, in <module><br class=""> test_property()<br class="">File "test.py", line 740, in test_property<br class=""> print(a.mine())<br class="">TypeError: Required argument 'fget' (pos 1) not found<br class="">turin:pkdiagram patrick$<br class=""></blockquote>Properties are class objects not instance objects.<br class="">Phil<br class=""></blockquote></blockquote><br class=""></blockquote>_______________________________________________<br class="">PyQt mailing list PyQt@riverbankcomputing.com<br class="">https://www.riverbankcomputing.com/mailman/listinfo/pyqt<br class=""></blockquote></div></div></blockquote></div><br class=""></div></body></html>