When calling a function via a signal connection as in
mybutton.clicked.connect(myfunction)
the function is called with all its arguments set to False. Even though default arguments have been set. Is this the expected behavior?
The code below shows a simple example. For my particular case having all arguments set to False is not a big issue as I can simply catch it with an if statement. However I feel that knowledge on why it is implemented this way may be helpful for further projects.
This simple program shows the behavior I would expect.
def function(foo=None):
print(foo)
function()
None
With this minimal Qt example however, the functions argument is no longer 'None', but 'False'.
import sys
from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QPushButton, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
btn = QPushButton("button",self)
gridLayout.addWidget(btn)
btn.clicked.connect(self.function)
def function(self, foo=None):
print(foo)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
False
The question, i guess, is: Do I have to live with no having the default values available in my functions (i.e. do I need to reset them once I notice that they were set to False) or is there any smarter way to handle it?