0

If multiple PyQt5 QTimer's are assigned the same callback function, is there any clean way (within the bounds of the PyQt architecture) to determine, in the callback, which Timer has expired?

eklektek
  • 1,083
  • 1
  • 16
  • 31
  • 1
    Use [`self.sender()`](https://doc.qt.io/qt-5/qobject.html#sender) (assuming the receiver function is a method of a QObject subclass), or just use a lambda, ensuring that a reference to the argument is kept, so use keyword arguments if the connection is done in a loop. – musicamante May 18 '23 at 21:01
  • @musicamente thanks again for you knowledge - just to demystify the lambda solution of a curious reader: self.timer0.timeout.connect(lambda timer_id=0: self.timer_timeout(timer_id)) then the slot: def timer_timeout(self, timer_id: int): – eklektek May 19 '23 at 07:28
  • While that syntax **may** be valid (as long as that `0` value of `timer_id` actually corresponds to that `self.timer0` reference), it also is misleading. The very concept behind using keyword arguments in lambdas is to preserve the scope of a reference. Besides, if you actually had local (non overwritten) references to those `timer_id` objects, using the keyword is pointless (just do `lambda: self.timer_timeout(0)` or `lambda: self.timer_timeout(timer0_id)`); OTOH, if you're creating timers in a for/while loop, creating instance attributes for them at every iteration is **wrong**. – musicamante May 22 '23 at 01:36

0 Answers0