0

When I load a dialog (QMainWindow) window from within my mainwindow (QMainWindow), it loads without layout, even though the setupUi() function is called.

The important pieces of code are here below, click here for pastebin link to full code

class Ui_Dialog(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_Dialog, self).__init__(parent)
        self.setupUi(self)
    def setupUi(self, Dialog):
        ...

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.show()
        ....
    def setupUi(self, Form):
        ...
        self.auto_sap_btn = QPushButton(Form)
        self.auto_sap_btn.setGeometry(QRect(0, 0, 61, 25))
        self.auto_sap_btn.setObjectName('auto_sap_btn')
        self.auto_sap_btn.clicked.connect(self.openDialog)

    def openDialog(self):
        self.window = Ui_Dialog(self)
        self.window.setupUi(self.window)
        self.window.move(600, 500)
        self.window.show()

Right now my dialog looks like this:

Failed dialog layout

When I load the dialog on its own from its own script created by:

pyuic5 -x dialog.ui -o dialog.py

it looks like this:

Proper dialog layout

What am I missing?

1 Answer 1

1

When you create a design based on a Template in Qt Designer, then when you have to pass the appropriate widget, when you created Ui_Dialog you surely used Dialog with Buttons Right so in this case you should use a QDialog instead of QMainWindow:

class Ui_Dialog(QDialog): # change QMainWindow to QDialog
    def __init__(self, parent=None):
        super(Ui_Dialog, self).__init__(parent)
        self.setupUi(self)
        [...]

Another mistake is to use the setupUi() method a second time since this method is responsible for filling in the widget, by calling it 2 times you will be adding more widgets unnecessarily:

def openDialog(self):
    self.window = Ui_Dialog(self)
    self.window.move(600, 500)
    self.window.show()
Sign up to request clarification or add additional context in comments.

1 Comment

You're completely right. I totally missed the 2nd time setupUi was ran. I followed this tutorial (youtube.com/watch?v=dRRpbDFnMHI), and he used QMainWindow, but after changing it to QDialog it works like a charm. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.