How to create a modal window in pyqt?

28,558

QDialog has setModal() as found here.

As the docs state:

By default, this property is False and show() pops up the dialog as modeless. Setting this property to true is equivalent to setting QWidget.windowModality to Qt.ApplicationModal.

As @sebastian noted you could use exec(). However it is better to use exec_() as the one sebastian mentioned is also a python call.

Example:

my_dialog = QDialog(self) 
my_dialog.exec_()  # blocks all other windows until this window is closed.

If this doesn't help please post your code and I will have a look.

Share:
28,558
user3135832
Author by

user3135832

Updated on July 13, 2020

Comments

  • user3135832
    user3135832 almost 4 years

    I looked into the documentation and i found 'self.setWindowModality(QtCore.Qt.WindowModal)'.
    I added this function to my 'init' function, but however still was not able to create a modal dialog box.

    Any help will be appreciated,
    Thank You.

  • user3135832
    user3135832 almost 10 years
    Sorry for wasting your time. I realised that what i was looking for was a system modal dialog, i should have read the docs properly. I'll edit the question. I apologise, but do you know if it is possible to create a system modal dialog in ppyqt for linux. Thanks.
  • Shadow9043
    Shadow9043 almost 10 years
    By System Modal you mean that your window should be on top of every other applications window? This is generally a bad idea and will end up frustrating your end user(s).
  • user3135832
    user3135832 almost 10 years
    Its actually a license agreement for a linux distro.
  • sebastian
    sebastian almost 10 years
    FWIW: I of course meant to use exec_, I just happen to use the Qt reference usually. Since exec not only a python call but a keyword - the exec method can't exist in python anyway.
  • bactone
    bactone almost 3 years
    how to show a dialog blocks only some windows rather than the entire application?