How to select a qComboBox programmatically in Simple Filters module

Hi,everyone. I’m trying to using code to substitute a series of mouse clicking and I get into trouble, please guide me.
In the first step, I need to select the first item in ‘OutputVolume’.(‘OutputVolume’ is a qComboBox).
In the second step, I need to select another, it means the second item in ‘OutputVolume’.

The apperance of this problem is when I click every qComboBox and select, every thing goes right, however, when I set the qComboBox programmly, it seems like I haven’t changed the ‘OutputVolume’ though the words showed is changed. And the result by program is same as when I only do the first step by clicking, but skip the second step.

I guess the problem is, in the second step, these 2 lines below,

OutputVolume=SimpleFiltersWidgetWhole[7]
OutputVolume.setCurrentNodeIndex(1)

which seems to only change what is showed visually in the qComboBox, but don’t truely get the item selected. In other word, it seems I need one more ‘click’ on the item to get it selected.

I loaded 2 .nii as volume.Hi,everyone. I’m trying to using code to substitute a series of mouse clicking and I get into trouble, please guide me.
In the first step, I need to select the first item in ‘OutputVolume’.(‘OutputVolume’ is a qComboBox).
In the second step, I need to select another, it means the second item in ‘OutputVolume’.

The apperance of this problem is when I click every qComboBox and select, every thing goes right, however, when I set the qComboBox programmly, it seems like I haven’t changed the ‘OutputVolume’ though the words showed is changed. And the result by program is same as when I only do the first step by clicking, but skip the second step.

I guess the problem is, in the second step, these 2 lines below,
OutputVolume=SimpleFiltersWidgetWhole[7]
OutputVolume.setCurrentNodeIndex(1)
which seems to only change what is showed visually in the qComboBox, but don’t truely get the item selected. In other word, it seems I need one more ‘click’ on the item to get it selected.

I loaded 2 .nii as volume.


my code below

#First flip
slicer.modules.simplefilters.widgetRepresentation()
slicer.modules.SimpleFiltersWidget.searchBox.setText(‘FlipImageFilter’)
SimpleFiltersWidgetWhole=slicer.modules.SimpleFiltersWidget.filterParameters.widgets
InputVolume=SimpleFiltersWidgetWhole[0]
InputVolume.setCurrentNodeIndex(0)
FlipAxesSpinbox1=FlipAxesSpinbox[1]#SagittalplaneFlip
FlipAxesSpinbox1.setValue(1)
FlipAxesSpinbox2=FlipAxesSpinbox[2]#CoronalplaneFlip
FlipAxesSpinbox2.setValue(0)
FlipAxesSpinbox3=FlipAxesSpinbox[3]#HorizontalplaneFlip
FlipAxesSpinbox3.setValue(0)
#setFlipAboutOriginchecked
FlipAboutOrigin=SimpleFiltersWidgetWhole[4]
FlipAboutOrigin.setChecked(1)
#setLabelMapchecked

LabelMap=SimpleFiltersWidgetWhole[9]
LabelMap.setChecked(0)
slicer.modules.SimpleFiltersWidget.applyButton.clicked()

#Secondflip
InputVolume=SimpleFiltersWidgetWhole[0]
InputVolume.setCurrentNodeIndex(1)
OutputVolume=SimpleFiltersWidgetWhole[7]
OutputVolume.setCurrentNodeIndex(1)
FlipAxes=SimpleFiltersWidgetWhole[2]#FlipAxes
FlipAxes.coordinates
FlipAxes.children()
FlipAxesSpinbox=FlipAxes.children()

FlipAxesSpinbox1=FlipAxesSpinbox[1]#SagittalplaneFlip
FlipAxesSpinbox1.setValue(1)
FlipAxesSpinbox2=FlipAxesSpinbox[2]#CoronalplaneFlip
FlipAxesSpinbox2.setValue(0)
FlipAxesSpinbox3=FlipAxesSpinbox[3]#HorizontalplaneFlip
FlipAxesSpinbox3.setValue(0)

#setFlipAboutOriginchecked

FlipAboutOrigin=SimpleFiltersWidgetWhole[4]

FlipAboutOrigin.setChecked(1)
#setLabelMapchecked
LabelMap=SimpleFiltersWidgetWhole[9]
LabelMap.setChecked(1)

slicer.modules.SimpleFiltersWidget.applyButton.clicked()

Rather than trying to script the GUI, you could just use the SimpleITK code directly:

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#running-an-itk-filter-in-python-using-simpleitk

1 Like