I tried to make a simple example to help understand how the concept of QSignalMapping works in PySide. I would like to dynamically create a series of buttons by iterating through a loop, and when the user pushes one of the buttons, I can activate a method that returns the appropriate label for the button that was pressed.
from PySide2 import QtWidgets,QtCore,QtGui
fruit_list = ["apples","oranges","pears"]
def fruit_button_event():
print "this is the pressed button's label"
def main():
for fruit in fruit_list:
fruit_button = QtWidgets.QPushButton(fruit)
fruit_button.clicked.connect(lambda:fruit_button_event())
main()
In the next part I show an example how to use QSignalMapper:
Remember that from Qt 5.10 QSignalMapper is deprecated:
The same functionality in python can be obtained with functools.partial(...):
Or with lambda:
Or QButtonGroup: