[파이썬] PyQt 다이얼로그 (`QDialog`)

In PyQt, a dialog is a window that is used to interact with the user and receive input or display information. The QDialog class provides a base for creating custom dialogs in PyQt applications. In this blog post, we will explore how to create and use QDialog in Python.

Creating a Simple QDialog

To create a QDialog in Python, we need to follow these steps:

  1. Import the necessary modules:
    from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout
    
  2. Create a subclass of QDialog:
    class MyDialog(QDialog):
     def __init__(self):
         super().__init__()
    
  3. Inside the subclass, create the user interface using PyQt widgets. For example, we can add a QLabel and a QPushButton:
         self.label = QLabel("Hello World!")
         self.button = QPushButton("Click Me!")
    
  4. Set the layout for the dialog and add the widgets to it:
         layout = QVBoxLayout()
         layout.addWidget(self.label)
         layout.addWidget(self.button)
         self.setLayout(layout)
    
  5. Create an instance of the QApplication class and our custom QDialog class:
    app = QApplication([])
    dialog = MyDialog()
    
  6. Show the dialog using the exec_() method:
    dialog.exec_()
    

Here’s the complete code for a simple QDialog:

from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout

class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        
        self.label = QLabel("Hello World!")
        self.button = QPushButton("Click Me!")
        
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)

app = QApplication([])
dialog = MyDialog()
dialog.exec_()

Customizing QDialog

We can customize the appearance and behavior of a QDialog by subclassing it and adding our own methods and signals. For example, we may want to add actions to be performed when the button is clicked. To do this, we can connect the clicked signal of the button to a custom method:

from PyQt5.QtCore import Qt

class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        
        self.label = QLabel("Hello World!")
        self.button = QPushButton("Click Me!")
        
        self.button.clicked.connect(self.on_button_click)
        
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)
        
    def on_button_click(self):
        self.label.setText("Button Clicked!")
        self.button.setText("Clicked!")
        self.button.setEnabled(False)
        self.setWindowFlag(Qt.WindowStaysOnTopHint)

app = QApplication([])
dialog = MyDialog()
dialog.exec_()

In the above example, when the button is clicked, the on_button_click method is called. It updates the label text, changes the button text and disables the button. It also sets the WindowStaysOnTopHint flag so that the dialog stays on top of other windows.

Conclusion

In this blog post, we learned how to create and customize a QDialog in PyQt using Python. We explored the basic steps involved in creating a simple dialog and saw how we can customize its appearance and behavior. With this knowledge, you can now create your own custom dialogs in your PyQt applications.