# Add a menu to sliceController

**URL:** <https://discourse.slicer.org/t/add-a-menu-to-slicecontroller/20472>\
**Category:** Development\
**Created:** [November 3, 2021, 1:11pm UTC](https://discourse.slicer.org/t/add-a-menu-to-slicecontroller/20472 "2021-11-03T13:11:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [November 3, 2021, 1:11pm UTC](https://discourse.slicer.org/t/add-a-menu-to-slicecontroller/20472/1 "2021-11-03T13:11:02Z")

</div>

Hi. This is my code to add a menu to the Red Slice controller:

```auto
toolButton = qt.QToolButton()

toolButton = qt.QToolButton()
toolButton.setText('T')
slicer.app.layoutManager().sliceWidget("Red").sliceController().barLayout().insertWidget(0,toolButton)

myMenu = qt.QMenu(toolButton)
toolButton.setMenu(myMenu)

pushButton = qt.QPushButton('mybutton')
widgetAction = qt.QWidgetAction(myMenu)
widgetAction.setDefaultWidget(pushButton)

myMenu.addAction(widgetAction)
myMenu.addAction("testAction")

```

It doesn’t work. The actions do not show when you click the toolButton. Could you help?

---

<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:** [November 3, 2021, 6:22pm UTC](https://discourse.slicer.org/t/add-a-menu-to-slicecontroller/20472/2 "2021-11-03T18:22:22Z")

</div>

The default popup mode is `DelayedPopup` for a QToolButton. I believe you have missed this as showing the action does work with your code when long pressing on the toolbutton.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/b/5bd3ab968845851303ff415989a245ee19988394.png)

```python
# Create ToolButton and add to sliceController
toolButton = qt.QToolButton()
slicer.app.layoutManager().sliceWidget("Red").sliceController().barLayout().insertWidget(0,toolButton)
# Create QMenu and add QActions to it
myMenu = qt.QMenu(toolButton)
myActionT = qt.QAction("T")
myActionTest = qt.QAction("Test")
myMenu.addAction(myActionT)
myMenu.addAction(myActionTest)
# Add Menu to ToolButton
toolButton.setMenu(myMenu)
# Want to define one of the menu items so it is triggered on toolbutton press? set it as default action
toolButton.setDefaultAction(myActionT)

# Default to show menu is DelayedPopup method which requires long press
# Long press is to differentiate it from pressing ToolButton (the default QAction)

# Want a different method to show menu?
# Other options include arrow next to button to open menu
toolButton.setPopupMode(qt.QToolButton.MenuButtonPopup)
# Open the menu by clicking the toolbutton rather than triggering the default QAction
toolButton.setPopupMode(qt.QToolButton.InstantPopup)

```
