Modify properties of the segment editor (maximumHeight of ctkExpandableWidget)

Hello,

I am a beginner at Slicer (1 week today), so I apologize if my question is simple. I am building a small module in which I would like to include the Segment Editor. In particular, I would like to set the maximum height of the ctkExpandableWidget object (i.e., the list of the segmentations) so as not to make it too long.

The following does what I want when executed in the Python console

segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.show()
segmentEditorWidget.layout().itemAt(2).widget().setMaximumHeight(100)

#or this
segmentEditorWidget.children()[13].setMaximumHeight(100)

However, when I put the same code in my .py file and insert it into my module, the ctkExpandableWidget object still behaves like the default ctkExpandableWidget (i.e., it can expand much further than this).

There is more code in my .py file (I followed a couple tutorials that also use the Segment Editor Widget in them), namely:

segmentationNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
segmentationNode.CreateDefaultDisplayNodes()

segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)

#inserts into a QGroupBox
groupBoxSegmentation = qt.QGroupBox("Segmentation")
segmentationLayout = qt.QVBoxLayout()
segmentationLayout.addWidget(segmentEditorWidget)
groupBoxSegmentation.setLayout(segmentationLayout)

Perhaps there is code in here that is overwriting the maximumHeight that I am trying to force?

Thank you for your help, and any comments to improve the above code are welcome!
EricM

There’s an open issue related to layout sizing of the segment editor widget which is what you are describing. This work has stalled, but you might want to look into helping pick this back up.

See https://github.com/Slicer/Slicer/pull/1085

Also, in general I’d advise against using pixel values for setting sizes because the wide range of DPIs of the current screens. Use ratios, or multiples of the sizeHint that you can get from certain elements.

Qt5 can scale sizes specified in pixels (they are now called “device independent pixels”) - see https://doc.qt.io/qt-5/highdpi.html. So, in theory, specifying size simply in pixels may work well on a variety of displays and resolutions.

Hm. I kind of liked using size hints for sizing because it allowed to be very precise compared to UI elements in whatever style. So you say we can now use pixels again in Slicer core too? I think we moved away from that completely.

I’d also vote for hints and ratios - I think those device independent pixels sound error prone.

When you use the Qt designer then most sizes and size hints are defined in device-independent pixels. With the automatic pixel scaling, simple layouts often work well enough. I would try to stick to these simple layouts whenever it is possible.

If there are complicated UI mechanisms or fine-tuned layout behavior is needed then of course it makes sense compute sizes from ratios, font sizes, or other metrics.

Thanks for these tips.
@jamesobutler - I will look at the link you provided from @Johan_Andruejol.
@Johan_Andruejol - is the SegmentEditorUIFix.zip file you provided in this link after the modifications on Jan 31, or is it from the original post on Jan 30? If it is after the second round of modifications, I can try to see if it fits in my module better.

Thanks again,
Eric

The archive provided by @Johan_Andruejol contains small files created on Jan 25 and useful only to test the changes proposed in the associated PR https://github.com/Slicer/Slicer/pull/1085