PyQt5 - QDialog Class


A QDialog widget presents a top level window mostly used to collect response from the user. It can be configured to be Modal (where it blocks its parent window) or Modeless (the dialog window can be bypassed).

PyQt API has a number of preconfigured Dialog widgets such as InputDialog, FileDialog, FontDialog, etc.


In the following example, WindowModality attribute of Dialog window decides whether it is modal or modeless. Any one button on the dialog can be set to be default. The dialog is discarded by QDialog.reject() method when the user presses the Escape key.

A PushButton on a top level QWidget window, when clicked, produces a Dialog window. A Dialog box doesn’t have minimize and maximize controls on its title bar.

The user cannot relegate this dialog box in the background because its WindowModality is set to ApplicationModal.

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

def window():
   app = QApplication(sys.argv)
   w = QWidget()
   btn = QPushButton(w)
   btn.setText("Hello World!")
   w.setWindowTitle("PyQt Dialog demo")

def showdialog():
   dlg = QDialog()
   b1 = QPushButton("ok",dlg)
   dlg.setWindowTitle("Dialog") 9. PyQt5 — QDialog Class

if __name__ == '__main__':

The above code produces the following output. Click on button in main window and dialog box pops up −

QDialog Class Output