Issue
For a subclass of QObject I'd like to define a property. On change, a signal should be emitted. According to the documentation, something like this: p = Property(int, _get_p, _set_p, notify=p_changed)
should work, but the signal is not emitted by the change. Full example here:
from PySide2.QtCore import QObject, Property, Slot, Signal
class A(QObject):
p_changed = Signal()
def __init__(self):
super(A, self).__init__()
self._p = 0
def _get_p(self):
print(f"Getting p (val: {self._p})")
return self._p
def _set_p(self, v):
print(f"Setting new p: {v}")
self._p = v
p = Property(int, _get_p, _set_p, notify=p_changed)
if __name__ == "__main__":
import sys
from PySide2.QtWidgets import QApplication, QWidget
app = QApplication([])
class W(QWidget):
def __init__(self, *args):
super().__init__(*args)
self.a = A()
self.a.p_changed.connect(self.test_slot)
print(self.a.p)
self.a.p = 42
# Expecting "Got notified"!
print(self.a.p)
# This would print "Got notified":
# self.a.p_changed.emit()
@Slot()
def test_slot(self):
print("Got notified")
w = W()
w.show()
sys.exit(app.exec_())
Solution
That you associate a signal to a QProperty does not imply that it will be emitted automatically but that you have to emit it explicitly.
def _set_p(self, v):
if self._p != v:
print(f"Setting new p: {v}")
self._p = v
self.p_changed.emit()
For more information read The Property System.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.