Create new qSlicerWidget

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.

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:

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:

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

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

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

I solved the problem in the following way.

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)
1 Like