
- 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 - QDialogButtonBox Widget
The QDialogButtonBox tool makes it easy to include buttons, in dialog boxes. It streamlines the creation of dialog button setups like OK, Cancel, Apply and more. With this feature developers can maintain button positioning and actions, on platforms.
Methods used in QDialogButtonBox
Some commonly used methods of the QDialogButtonBox widget along with their descriptions are as follows −
Method | Description |
---|---|
addButton() | Adds a button to the button box. |
button() | Returns the button associated with the specified role. |
setStandardButtons() | Sets the standard buttons to be added to the button box. |
standardButton() | Returns the standard button that was clicked. |
Example 1: Creating a Simple QDialogButtonBox
In this example, we create a QDialogButtonBox with OK and Cancel buttons using the QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel argument. We connect the accepted and rejected signals of the button box to the accept and reject slots of the dialog, respectively.
import sys from PyQt6.QtWidgets import QApplication, QDialog, QDialogButtonBox, QVBoxLayout class Dialog(QDialog): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() buttonBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) buttonBox.accepted.connect(self.accept) buttonBox.rejected.connect(self.reject) layout.addWidget(buttonBox) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) dialog = Dialog() dialog.exec() sys.exit(app.exec())
Output
The above code produces the following output −

Example 2: Using Standard Buttons
In this example, we create a QDialogButtonBox and set standard buttons (Save, Cancel, and Discard) using the setStandardButtons() method. We connect the clicked signal of the button box to a custom slot buttonClicked, which identifies the clicked button using standardButton() and performs specific actions based on the button's role.
import sys from PyQt6.QtWidgets import QApplication, QDialog, QDialogButtonBox, QVBoxLayout class Dialog(QDialog): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() buttonBox = QDialogButtonBox() buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Save | QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Discard) buttonBox.clicked.connect(self.buttonClicked) layout.addWidget(buttonBox) self.setLayout(layout) def buttonClicked(self, button): role = buttonBox.standardButton(button) if role == QDialogButtonBox.Save: print("Save clicked") elif role == QDialogButtonBox.Cancel: print("Cancel clicked") elif role == QDialogButtonBox.Discard: print("Discard clicked") if __name__ == '__main__': app = QApplication(sys.argv) dialog = Dialog() dialog.exec() sys.exit(app.exec())
Output
The above code produces the following output −
