# Create new qSlicerWidget

**URL:** <https://discourse.slicer.org/t/create-new-qslicerwidget/33132>\
**Category:** Development\
**Tags:** python, loadable-module\
**Created:** [November 30, 2023, 4:38am UTC](https://discourse.slicer.org/t/create-new-qslicerwidget/33132 "2023-11-30T04:38:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Manuel\_Montan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_manuel_montan/32/68515_2.png) [@Victor\_Manuel\_Montan](https://discourse.slicer.org/u/Victor_Manuel_Montan)\
**Post date:** [November 30, 2023, 4:38am UTC](https://discourse.slicer.org/t/create-new-qslicerwidget/33132/1 "2023-11-30T04:38:21Z")

</div>

I am developing a new qSlicerWidget that should be shown when a button that is part of a qMRMLWidget is clicked. When the new qSlicerWidget is created empty, it is possible to see the new window without any elements inside.

 ![Captura de pantalla 2023-11-29 a la(s) 9.00.33 a.m.](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/d/5d867ab7e0dd75c89ac7f7d9771f7e5fd7cc7bcf.jpeg)

But when I try to load a UI created from qtStudio in a UI file that contains a widget. The class is in a new file as shown below:

```python
import slicer
import os

class TutorialGUI(slicer.qSlicerWidget):
    def __init__ (self, parent=None):
        super(). __init__ (parent)
        print(self)
        dir_path = os.path.dirname( __file__ )
        
        self.Widget = slicer.util.loadUI(dir_path+'/../Resources/UI/TutorialGUI.ui')
        self.layout.addWidget(self.Widget)

    def showEvent(self, event):
        # Do something when the window is shown
        print("The window is shown!")

```

in python console shows following error message:

```auto
Traceback (most recent call last):
  File "/Users/victormontanoserrano/Documents/3DSlicer/NewGui/NewGUI/NewGUI.py", line 141, in __init__
    self.window = TutorialGUI()
  File "/Users/victormontanoserrano/Documents/3DSlicer/NewGui/NewGUI/Lib/TutorialGUI.py", line 11, in __init__
    self.layout.addWidget(self.Widget)
AttributeError: 'builtin_qt_slot' object has no attribute 'addWidget'
[Qt] qSlicerPythonCppAPI::instantiateClass - ["NewGUIWidget"] - Failed to instantiate scripted pythonqt class "NewGUIWidget" 0x7fcbb551fba0

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 30, 2023, 4:46am UTC](https://discourse.slicer.org/t/create-new-qslicerwidget/33132/2 "2023-11-30T04:46:51Z")

</div>

The error message tells that `self.layout` is a slot. Probably you wanted to write `self.layout()` instead.

---

<div class="post-metadata">

**Author:** ![Victor\_Manuel\_Montan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_manuel_montan/32/68515_2.png) [@Victor\_Manuel\_Montan](https://discourse.slicer.org/u/Victor_Manuel_Montan)\
**Post date:** [December 1, 2023, 7:08pm UTC](https://discourse.slicer.org/t/create-new-qslicerwidget/33132/3 "2023-12-01T19:08:27Z")

</div>

Thanks for the comment. I made the change but it was not the solution.

I solved the problem in the following way.

```python
import qt
import slicer
import os

class TutorialGUI(slicer.qSlicerWidget):
    def __init__ (self, parent=None):
        super(). __init__ ()

        # Load a UI file
        dir_path = os.path.dirname( __file__ )
        self.uiWidget = slicer.util.loadUI(dir_path+'/../Resources/UI/TutorialMaker.ui')
        
        # Create a new accessible qt layout component
        self.newlayout = qt.QVBoxLayout()
        
        # Add Widget from IU file to new layout
        self.newlayout.addWidget(self.uiWidget)

        # Set self layout with UI components 
        self.setLayout(self.newlayout)

```
