0

I am trying to set up a signal-slot arrangement in PyQt where the signal transmits a lot of information. I want to use a QObject as a carrier by setting the various information I want to transmit as attributes of the QObject. In theory, this should work - there is a QSignalMapper.setMapping() overload which takes a sender and a QObject as arguments.

Here's the reduced code:

self.mapper = QtCore.QSignalMapper()
self.timeline.finished.connect(self.mapper.map)
carrier = QtCore.QObject()
carrier.contents = (item1, item2)
self.mapper.setMapping(self.timeline, carrier)
self.portalMapper.mapped.connect(self.report)

def report(self, carrierObject):
    print 'Triggered'

Unfortunately it doesn't work. I've traced the problem to the setMapping function by process of elimination.

This same scheme will work just fine if I switch out the QObject with an int. It also doesn't have anything to do with the attributes I added to the QObject - using a fresh-out-of-the-box QObject causes the same issue.

It seems like there is something going on here with this specific overload of the setMapping function. Any ideas about what the issue is here?

Grav
  • 347
  • 1
  • 2
  • 15
  • 1
    There's no value in using `QSignalMapper`. Just define a custom signal and send the data directly. – ekhumoro Jun 13 '17 at 13:04
  • I'm unsure how to do that, @ekhumoro ... The signal my code is waiting for is the Qt-standard `QTimeline.finished`, and has to be, since I'm waiting for a timer to conclude before proceeding with the next step. I could connect the `finished()` signal to a function which in turn sends a custom signal... but wouldn't that just be recapitulating the purpose of the a QSignalMapper, to receive a signal and send a new signal with a new argument? – Grav Jun 13 '17 at 15:59
  • 1
    Sure - but it does it a lot more simply. Does your code really need to send a signal at all, though? In your example, there is only one consumer of the signal (`self`), so emitting a signal appears to be completely redundant. All you're doing is sending a pointer to an object, not the data itself (i.e. nothing is ever copied). You might as well keep a reference to the data itself as an attribute of `self`, and then access that directly in a slot connected to the `finished` signal. – ekhumoro Jun 13 '17 at 16:29

1 Answers1

0

Thanks to @ekhumoro's suggestion to skip the QSignalMapper approach entirely and just create a custom signal. Worked like a charm.

Grav
  • 347
  • 1
  • 2
  • 15