Search code examples
qtpyqtpysidesignature

Check PyQt/PySide method signature


QWidget (PySide) constructor accepts f keyword, but QWizard has flags argument instead of f. Is it possible to inspect method signature beforehand? I want to be able to pass flags to any QWidget subclass.


Solution

  • As you mentioned QWidget accepts f, and QDialog also accepts f. QWizard appears to be the odd one out that uses flags. Since inspect.getfullargspec() does not work with PySide objects, I'd recommend making a helper function if you need to determine this at run time. The helper will have to determine whether to use the f or flags keyword argument based upon a known list of widget classes.

    from PySide2 import QtWidgets
    
    FLAGS_WIDGETS = (QtWidgets.QWizard,)
    
    def flags_kwarg(cls, flags):
        if issubclass(cls, FLAGS_WIDGETS):
            key = 'flags'
        else:
            key = 'f'
        return {key: flags}
    

    This can then be used as:

    from PySide2 import QtCore
    
    cls = QtWidgets.QWizard # or some other widget class
    flags = QtCore.Qt.WindowFlags()
    kwargs = flags_kwarg(cls, flags)
    
    widget = cls(parent, **kwargs)