I am currently building an interface using PyQt5 and I was wondering if there is an elegant way to connect several signals to the same function?
The straight-forward solution is this:
self.ui.button1.clicked.connect(self.Function)
self.ui.button2.clicked.connect(self.Function)
self.ui.button3.clicked.connect(self.Function)
self.ui.button4.clicked.connect(self.Function)
but is there a nicer, more readable option? For example something that would look like:
self.Function.connect(self.ui.button1.clicked,
self.ui.button2.clicked,
self.ui.button3.clicked,
self.ui.button4.clicked)
Also I started reading about QSignalMapper
s but if they can be avoided it's nice.
Thank you in advance!
You could make a function that takes a tuple or list of buttons as an argument, then connects the click signal for each one.
self.connect_buttons((self.ui.button1, self.ui.button2, self.ui.button3, self.ui.button4))
def connect_buttons(self, button_tup):
for button in button_tup:
button.clicked.connect(self.Function)