QDialog: show() vs open()

14,275

Solution 1

show() will just show you the dialog without affecting the other windows in your program. open() will show() the window + prevent other windows from being accessible through setWindowModality(), i.e., it becomes a modal window.

This is useful if you want to open a file, for example, and you don't want the user to be able to do anything in the program until a file is chosen and that dialog is closed.

Quoting from Qt's manual:

A modal dialog is a dialog that blocks input to other visible windows in the same application. Dialogs that are used to request a file name from the user or that are used to set application preferences are usually modal. Dialogs can be application modal (the default) or window modal.

When an application modal dialog is opened, the user must finish interacting with the dialog and close it before they can access any other window in the application. Window modal dialogs only block access to the window associated with the dialog, allowing the user to continue to use other windows in an application.

The most common way to display a modal dialog is to call its exec() function. When the user closes the dialog, exec() will provide a useful return value. Typically, to get the dialog to close and return the appropriate value, we connect a default button, e.g. OK, to the accept() slot and a Cancel button to the reject() slot. Alternatively you can call the done() slot with Accepted or Rejected.

Solution 2

As it is stated in the doc, QDialog::open()

Shows the dialog as a window modal dialog, returning immediately.

whereas QDialog::show(), which is in fact QWidget::show(), will only show your dialog as a standard, non-modal widget.

Share:
14,275
Jan Rüegg
Author by

Jan Rüegg

Computer scientist and passionate C++ developer.

Updated on February 10, 2023

Comments

  • Jan Rüegg
    Jan Rüegg over 1 year

    Whats the difference between QDialog::show() and QDialog::open()?

  • Jan Rüegg
    Jan Rüegg almost 8 years
    So is "dialog.open()" exactly the same as "dialog.setWindowModality(...);dialog.show();"?
  • The Quantum Physicist
    The Quantum Physicist almost 8 years
    @JanRüegg Yes. At least that's what the documentation says. I enable modality manually normally.