Dutchman
Dutchman

Reputation: 57

PyQt5 Dialog window opens without layout

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?

Upvotes: 0

Views: 224

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

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()

Upvotes: 1

Related Questions