I'm struggling to pass contextual information with a button clicked signal.
Here's a minimal example, which creates a widget with several numbered buttons. The message box should display the corresponding number when each button is clicked.
import sys
from PySide6 import QtCore, QtWidgets, QtGui
class Gui(QtWidgets.QWidget):
def __init__(self, parent=None, n_buttons=9):
super(Gui, self).__init__(parent)
self._mainLayout = QtWidgets.QVBoxLayout()
self.setLayout(self._mainLayout)
for n in range(n_buttons):
btn = QtWidgets.QPushButton(str(n))
self._mainLayout.addWidget(btn)
btn.clicked.connect(
lambda val=n: self.showMsg(val)
)
def showMsg(self, message):
msg = QtWidgets.QMessageBox()
msg.setText(str(message))
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.exec()
def main():
app = QtWidgets.QApplication([])
view = Gui()
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Instead the message box shows "False" whichever button I click.
I've tried implementing the signal using a lambda function, following the explanation here: https://www.learnpyqt.com/tutorials/transmitting-extra-data-qt-signals/
Thanks for any advice!