Customizing Python after extension wizard

I used the Slicer Extension Wizard to create two modules for my custom Slicer application. Instead of the inputSelector (qMRMLNodeComboBox), outputSelector (qMRMLNodeComboBox), imageThresholdSliderWidget (ctkSliderWidget), invertOutputCheckBox (QCheckBox), and invertedOutputSelector (qMRMLNodeComboBox). which are supplied as examples, I will have DataDirectory (ctkPathLineEdit) and PatientPrefix (QLineEdit).

I have some pretty good ideas on how to modify the MyNewModule.ui file accordingly. However, I am having trouble with the MyNewModule.py file. For example, what should the self.ui.DataDirectory.connect(...) call look like? (I am not that clear on what this does which likely is a big part of my problem.) Likewise what should the arguments for self.ui.PatientPrefix.connect(...) be? Thanks!

Have a read of this old post of mine seen below. I generally prefer the new style Qt signal/slot method of {object}.{signal_name}.connect({slot_name})

1 Like

It creates a connection between a Qt signal and a Python method. When the Qt object emits the signal then it calls the connected Python method. You can find the specification of signals in the header files or the generated documentation. Unfortunately, CTK documentation site is down, so you need to look up the signal parameters in source code:

  • google for ctkPathLineEdit, click on the first hit
  • search for signals
  • probably you are interested in this signal:
  • the connection code will look like this:
self.ui.DataDirectory.connect('currentPathChanged(QString)', self.someFunction)

Since Slicer is open-source and there are about 200 open-source extensions, mostly developed in Python, hosted on github, an alternative approach is to learn from working examples. You can find example for most API calls, for example you can search for ctkPathLineEdit currentPathChanged in Python code in entire github. One of the hits show how to create the connection:

I’ve added the information from @jamesobutler and my post to the Python FAQ:

Please have a look and suggest improvements that would help other developers.