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 −

pyqt qDialogButtonBox example 1

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 −

pyqt qDialogButtonBox example 2
Advertisements