3

I want to use custom widgets created with QtCreator by importing directly the .ui file. Currently my code works fine when I use pyuic5 to create a .py file. But I want to be able to import my widgets directly from the .ui file. Some of the working code :

main.py

import MyWidget

MainWindowUI, MainWindowBase = uic.loadUiType('main.ui')

class mainGUIWindow(MainWindowUI, MainWindowBase):
     def __init__(self, mainWindow):
         QtWidgets.QMainWindow.__init__(self)
         self.setupUi(self)

         self.myWidget = MyWidget.Ui_Form()
         self.myWidget.setupUi(self)

MyWidget.py (generated via pyuic5 -x MyWidget.ui -o MyWidget.py)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(271, 201)
        # The rest of the widget is down here

Is there a way to use LoadUi() or something similar to avoid the use of pyuic ? What I have tried so far:

main.py

class mainGUIWindow(MainWindowUI, MainWindowBase):
    def __init__(self, mainWindow):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self) # contains a QFrame called 'frame'

        # Creation of myWidget
        self.wid = myWidget()

        # Adding myWidget to a layout
        hbox = QtWidgets.QHBoxLayout()
        hbox.addWidget(self.wid)
        self.frame.setLayout(hbox)


class myWidget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        uic.loadUi('MyWidget.ui', self)

When I try to add my widget to a layout in my main window I get this error:

QWidget::setLayout: Attempting to set QLayout "" on QFrame "frame", which already has a layout

Gwendal Grelier
  • 526
  • 1
  • 7
  • 21
  • 1
    Take a look at this: https://stackoverflow.com/questions/2398800/linking-a-qtdesigner-ui-file-to-python-pyqt They use PyQt4 but it's the same in PyQt5 – GPhilo Jan 30 '18 at 10:30
  • I edited my post to show what I have tried. I can create a Widget class in my main.py file but I cannot add it properly into the main window – Gwendal Grelier Jan 30 '18 at 10:57
  • Look here for the new error (which doesn't seem related to your first question): https://stackoverflow.com/questions/25450598/qlayout-attempting-to-add-qlayout-to-qwidget-which-already-has-a-layout – GPhilo Jan 30 '18 at 11:08

1 Answers1

6

I found a solution for this problem even if it's not the ideal for me I think it can help others. I had to create a new class which inherits PyQt5.QtWidgets.QWidget during the __init__() of this widget, the loadUi() function is called to catch the widget.ui file.

class mainGUIWindow(MainWindowUI, MainWindowBase):
    def __init__(self, mainWindow):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)

        wid = myWidget()

        self.grid = QtWidgets.QGridLayout()
        self.grid.addWidget(wid,0,0)
        self.frame.setLayout(self.grid)


class myWidget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        uic.loadUi('Widget1.ui', self)
Gwendal Grelier
  • 526
  • 1
  • 7
  • 21