Call a widget's methods in Python


For the purposes of building a test, I am wondering whether there is a way to instantiate and call the methods from a class that inherits from qSlicerWidget​ (e.g. qSlicerTractographyDisplayWidget, which lives in SlicerDMRI, and which has its Qt Designer UI file here) programmatically from Python.

I am able to do the below:

m = slicer.util.mainWindow()
display_widget = slicer.util.findChildren(name="qSlicerTractographyDisplayModuleWidget")[0]

but when trying to call any of the qSlicerTractographyDisplayWidget methods, e.g.:

opacity = display_widget.opacity()

it looks like I am querying the wrong object:

AttributeError: qSlicerTractographyDisplayWidget has no attribute named 'opacity'

AttributeError: qSlicerTractographyDisplayWidget has no attribute named 'updateWidgetFromMRML'

What am I missing?


Hi - Qt methods are not wrapped by default. Only things like slots and properties are available unless you put Q_INVOKABLE in front of the methods name you want to make available.

Thanks Steve.

Maybe I’m wrong, but calling a public slot, e.g. setVisibility:

also results in the same error. Is there something else that I am missing?

There must be something wrong with either the wrapping or the way the wrapping libraries are being loaded in the extension. You could look at how things work in the core, where QObject signals/slots/properties/invokables are available in python. Maybe also look at other extensions that provide QObject/QWidget classes with wrapped methods. It could be something in the way the cmake files are set up.