# Qt switch button in Slicer

**URL:** <https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746>\
**Category:** Development\
**Created:** [February 2, 2022, 5:10am UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746 "2022-02-02T05:10:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [February 2, 2022, 5:10am UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/1 "2022-02-02T05:10:15Z")

</div>

Hi all,

I am thinking about using the qt switch button in Slicer as shown here [Switch QML Type | Qt Quick Controls 5.15.8](https://doc.qt.io/qt-5/qml-qtquick-controls2-switch.html). However, I could not find it in Slicer. Does Slicer incorporate the qt switch button function as python script?

Thank you!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 2, 2022, 1:07pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/2 "2022-02-02T13:07:58Z")

</div>

I’ve often thought we should be able to use Qt Quick in Slicer but it was never obvious how to do it. If someone has time to explore this it could be fun to try but for now I’d suggest using a `QCheckBox` instead.

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [February 2, 2022, 5:31pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/3 "2022-02-02T17:31:49Z")

</div>

Thank you for letting me know! I was thinking about using the switch button to toggle displaying some results. I’ll use QRadioButton or QCheckbox instead.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 2, 2022, 5:36pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/4 "2022-02-02T17:36:40Z")

</div>

> [@pieper](#):
>
> but it was never obvious how to do it.

I think it looks more modern and clear in places where you need to toggle visibility of something (see below). Of course checkbox or radio buttons can do the job, but I think it Switch is far more intuitive then explaining what that checkbox does in a tool tip.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/b/0bc00047321d3c91056310228aa652cff4980366.png)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 2, 2022, 5:41pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/5 "2022-02-02T17:41:59Z")

</div>

^In your case @muratmaga it would seem like some re-wording could be done where it would be:

"Show Mean point label: [] "

The checkbox shouldn’t require any additional tooltip. Should be intuitive that a checked field here would show the mean point label and an unchecked field would mean that it is not showing.

Just a suggestion considering the Switch object is not available to use.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 2, 2022, 5:46pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/6 "2022-02-02T17:46:43Z")

</div>

We used to have checkboxes in there, with a label that read **toggle mean shape visibility** followed by a checkbox. Surprisingly it wasn’t clear to the people. I think the checkboxes and stuff are going out of favor in UI design.

That’s why we now have button, which unfortunately don’t work really well in dark mode due to low contrast, when it is in pressed down state. Having sliding on/off switches would have been nice.

---

<div class="post-metadata">

**Author:** ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)\
**Post date:** [February 2, 2022, 5:49pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/7 "2022-02-02T17:49:53Z")

</div>

With Qt stylesheets, you can replace the rendered checkbox with an arbitrary image to create the switch look and feel

> <https://stackoverflow.com/questions/5962503/qt-checkbox-toolbutton-with-custom-distinct-check-unchecked-icons>

To do this:

- Add the images to your module as resources
- Create a stylesheet (.qss) for the checkbox design and add as a resource
- Add `checkbox.setStylesheet({the contents of the qss file})`
  - You can also just embed the qss as a string in the .py file instead of using a separate .qss

- [Example](https://github.com/PerkLab/SlicerSandbox/blob/c6ade46bf1dc284ec00d404218d477f817c4dd87/StyleTester/StyleTester.py#L120)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 2, 2022, 5:49pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/8 "2022-02-02T17:49:55Z")

</div>

Yeah the term “Toggle” implies a switch type. Which is why my terminology of “Show” may be the better term for now.

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [February 2, 2022, 6:13pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/9 "2022-02-02T18:13:08Z")

</div>

This looks great. Thank you!

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [February 3, 2022, 6:18pm UTC](https://discourse.slicer.org/t/qt-switch-button-in-slicer/21746/10 "2022-02-03T18:18:08Z")

</div>

Hi,

I wanted to ask did you encounter a segmentation fault when using any stylesheet applied (even through [SlicerSandbox](https://github.com/PerkLab/SlicerSandbox)) and you go to the Transform module and then switch to some other module?

I already [asked this question](https://discourse.slicer.org/t/segmentation-fault-with-any-applied-qt-stylesheet-when-switch-off-from-transform-module/21584) but got no feedback. Currently I’m unable to test it on Windows (slow PC and internet connection).
