`findChild` is not an attribute

I downloaded the latest build (4.13.0-2021-05-04) and now my .slicerrc.py is not working. This is because I’m trying to reach a button of SegmentEditorWidget:

show3DButton = getModuleGui( 'SegmentEditor' ).findChild( 'ctkMenuButton', 'Show3DButton' )

Why does this happen? There is a findChild() method of QObject (which the module widget should inherit from as far as I know). And it looks like Slicer is using findChild() also. It did work with 4.13.0-2021-01-20

PS: I see that getModuleGui() is deprecated in favour of getModuleWidget(), but that doesn’t work neither.

Try slicer.util.getModuleWidget('SegmentEditor')

Update: It won’t work either with your findChild, but this will:

slicer.util.getModuleWidget('SegmentEditor').editor.findChild( 'ctkMenuButton', 'Show3DButton' )

1 Like

That worked. Thanks!

So it looks like the class SegmentEditorWidget implemented in Python is no longer a QWidget, but instead contain qSlicerSegmentationsModuleWidget as a member editor. What’s behind this change? It is strange that getModuleWidget() is not returning a (Q)widget (but it did so before).

Is there an API documentation of the Python interface to Slicer? It is not easy to understand how the classes are intended to work.

BTW, do the Python-implemented classes dropping the prefixes vtk/mrml/q etc. as the C++ classes have?

The SegmentEditorWidget class has had the editor member since the first commit, so in that sense nothing changed. The reason for this difference is this commit in slicer.util, which for scripted module widgets returns the actual Python class instead of the C++ wrapper. The reason it worked for you is most probably that the Qt widgets in the editor module transitively had this C++ class as parent, however the Python widget class is not a QWidget.

I don’t think there is a complete Python API doc. You can use the C++ Doxygen in conjunction with the source code itself. And since Python works via interpreter, you can try everything first in Slicer’s Python interactor, and you can see what is available using the auto-complete function, and get documentation for the functions using help(className.functionName).

1 Like

Thank you! I’ve been using the C++ API (and source code) alot while learning Python scripting. Luckily I have a lot of C++ knowledge (but no Python, so I have actually learnt Python through Slicer :grinning:).

It looks like the Python wrapping of C++ use the same naming and therefore prefixes vtk/mrml/q/etc). But are the Python-only classes like SegmentEditorWidget using no prefixes, so this is a way to differate between C++ and Python-only classes?

I would say yes. As far as I know all C++ classes start with vtk or q, while all of the Python classes start with capital letter and generally without such prefix.

1 Like