1

I am working on a PyQt application and have encountered an issue where a parameter seems to be evaluated as False at runtime rather than None. Here's a snippet of the code:

def createMenu(self):
    """Overrides Main menu"""
    super().createMenu()
    action = QAction('Import to MyPlugin', self, triggered=self.importFileToMyPlugin)
    self.file_menu.insertAction(self.file_menu.actions()[6], action)

def importFileToMyPlugin(self, filename=None):
    """Overrides Import file"""
    from myplugin.myplugin.plugins.plugin_module import MyPlugin

    self.loadPlugin(MyPlugin)

    self.addSheet()
    w = self.getCurrentTable()
    w.importMyPluginFile(filename, dialog=True)
    return

In the importFileToMyPlugin function, I set the default value of the filename parameter to None. However, at runtime, it seems to become False. I can't figure out why this is happening.

Has anyone encountered this issue before? How can I ensure that the filename stays as None when it's not provided?

Any insight would be greatly appreciated. Thank you!

I have looked at an answer that suggested using the @QtCore.pyqtSlot() decorator to avoid this issue but I'm not convicend that's the easiest solution.

semsaco
  • 36
  • 3
  • Show us how you assert that the value is `False`. – Klaus D. Aug 25 '23 at 12:57
  • 1
    The [`triggered`](//doc.qt.io/qt-5/qaction.html#triggered) signal has a default argument corresponding to its checked state (even if the action isn't checkable) just like it happens with `clicked` for buttons. If you don't want that argument given in your function, either use the `pyqtSlot()` decorator without arguments (which *is* the easiest and correct solution for this case), use a lambda (`triggered=lambda: self.importFileToMyPlugin()`) or a separate function that calls the other without arguments. Alternatively, check the argument type: `if isinstance(filename, bool): filename = None`. – musicamante Aug 25 '23 at 13:17

0 Answers0