Ad

QMessageBox Position On QMessageBox.warning Call

- 1 answer

From current window I am trying to create a simple confirmation dialog using QMessageBox.

My code is:

res = QMessageBox.question(QMessageBox(currentWindow), "Question",  "Are you sure?", QMessageBox.Yes | QMessageBox.No)

The problem is that the confirmation dialog appears in absolutely random place on the screen and not on top of the parent window. I observe this behavior on MacOS 10.12. And it is also the case for QMessageBox.warning, QMessageBox.critical, and other static methods.

Is there any way to set the position of modal dialog to be relative to its parent?

Ad

Answer

The first parameter of QMessageBox::question() is the parent which will be used by the dialog box. A QDialog is centered (and modal) on its parent.

By doing QMessageBox.question(QMessageBox(currentWindow), you pass a new instance of QMessageBox which is not displayed.

Pass currentWindow instead:

res = QMessageBox.question(currentWindow, "Question", "Are you sure?", QMessageBox.Yes | QMessageBox.No)

But, you need to be sure that currentWindow is displayed and not None.

Ad
source: stackoverflow.com
Ad