
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QDialog Widget
The QDialog widget in PyQt serves as a base class for dialog windows. We can create custom dialog boxes with various functionalities such as input forms, message boxes, and more. QDialog inherits from QWidget which makes it flexible and can be customized for different use cases.
QDialog Widget: Methods and Descriptions
Method | Description |
---|---|
exec() | Executes the dialog synchronously. |
open() | Opens the dialog non-modally. |
accept() | Emits the accepted signal and closes the dialog. |
reject() | Emits the rejected signal and closes the dialog. |
setModal(bool) | Sets whether the dialog is modal. |
result() | Returns the result of the dialog. |
setDefaultButton(QPushButton) | Sets the default button for the dialog. |
setFixedSize(int, int) | Sets the fixed size of the dialog. |
setSizeGripEnabled(bool) | Sets whether the size grip is enabled. |
setWindowModality(Qt.WindowModality) | Sets the window modality for the dialog. |
Examples
Example 1: Simple Message Box
In the below example, we create a simple message box using QMessageBox. We then set the text of the message box to "Hello, PyQt!" and execute it using exec() function.
import sys from PyQt6.QtWidgets import QApplication, QDialog, QMessageBox class SimpleMessageBox(QDialog): def __init__(self): super().__init__() self.setWindowTitle("Simple Message Box") msg_box = QMessageBox() msg_box.setText("Hello, PyQt!") msg_box.exec() if __name__ == "__main__": app = QApplication(sys.argv) dialog = SimpleMessageBox() dialog.exec() sys.exit(app.exec())
Output
The above code produces the following output −

Example 2: Input Dialog
In the below example, we implement how to create an input dialog using QInputDialog. When we click the button a dialog asks the user to enter their name. The entered text is printed on the console.
import sys from PyQt6.QtWidgets import QApplication, QDialog, QLineEdit, QVBoxLayout, QPushButton, QInputDialog class InputDialog(QDialog): def __init__(self): super().__init__() self.setWindowTitle("Input Dialog") layout = QVBoxLayout() self.setLayout(layout) self.button = QPushButton("Get Input", self) self.button.clicked.connect(self.get_input) layout.addWidget(self.button) def get_input(self): text, ok = QInputDialog.getText(self, "Input Dialog", "Enter your name:") if ok: print("Your name is:", text) if __name__ == "__main__": app = QApplication(sys.argv) dialog = InputDialog() dialog.exec() sys.exit(app.exec())
Output
The above code produces the following output −

Advertisements