Access widget class from the test class in a Slicer plugin

Dear all,

Is it possible to access a widget class element from the test class in a slicer plugin? I know this is not how the test class works but I am controlling the logic functions with an extra variable taht indicates if the call from the widget class or from the test class.

When I try this:

class myClassTest(ScriptedLoadableModuleTest):
     def setUp(self):
           self.logic = myClassLogic()
           self.widg  = myClassWidget()

It creates a new instance of the widget instead of accessing the current widget.

I don’t believe there is a direct way to access it from the test class but you can get it through the slicer module:

volumeRenderingWidget = slicer.modules.volumerendering.widgetRepresentation()

1 Like

Thanks for your quick answer.

It seems the widget get new instance every time one click reload and test without destroying the old instance. This code gets the last instance:

In the widget class:

    self.mainCollapsibleBtn = ctk.ctkCollapsibleButton()

In the test class:

  widg   = slicer.modules.myModule.widgetRepresentation()
  wObjs = [x for x in widg.children()[1:] if x.windowTitle =="mainCollapsibleBtn"][-1].children()
  #getting the classes information
  for x in wObjs[1:]:
  #endfor x

By getting the classes information one can access the desirable class e.g.

   myButton = wObjs[3]

Slicer releases all references to the old widget, but if you keep references to it then it will not be destroyed. It is a debug/development feature anyway, so you don’t need worry about old widgets hanging around (if it becomes to confusing then just restart the application), but it makes sense to at least remove all observers in the widget’s cleanup() method.

By the way, you should never ever need to use children() or findWidget on your own module’s GUI. You have direct access to all widgets (something like, if you follow the current Python scripted module template).

1 Like