Search code examples
pythonuser-interfacepyqt6

PyQt6 Custom Dialog Content Not Appearing


When I run this code in my program, I expect to see an edit dialog when I click on the 'Edit Record' button. Instead, it shows me a blank dialog.

Code:

class EditDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Edit Record')
        self.setFixedSize(300, 300)

        layout = QVBoxLayout()

        index = main_window.table.currentRow()
        student_name = main_window.table.item(index, 1).text()
        self.student_name = QLineEdit(student_name)
        self.student_name.setPlaceholderText('Student Name')
        layout.addWidget(self.student_name)

        course = main_window.table.item(index, 2).text()
        self.student_course = QComboBox()
        courses = ["Biology", "Math", "Astronomy", "Physics"]
        self.student_course.addItems(courses)
        self.student_course.setCurrentText(course)
        layout.addWidget(self.student_course)

        phone = main_window.table.item(index, 3).text()
        self.student_phone = QLineEdit(phone)
        self.student_phone.setPlaceholderText('Student Phone Number')
        layout.addWidget(self.student_phone)

        button = QPushButton('Edit Record')
        button.clicked.connect(self.edit_student)
        layout.addWidget(button)

Here is the contextual code:

    @staticmethod
    def edit():
        edit_dialog = EditDialog()
        edit_dialog.exec()

I wanted to see the edit GUI, but instead, I got a blank screen. Can someone tell me what is wrong with my code. Image of output screen


Solution

  • Don't forget

    self.setLayout(layout)
    

    at the end of your init method.