4

I have a dialog with a big textbox. I want the user to be able to maximize the dialog. I already tried the following options (XML_Editor is a QDialog instance):

XML_Editor.setWindowFlags(QtCore.Qt.Window or QtCore.Qt.WindowMaximizeButtonHint) -- shows the Maximize button but doesn't center the dialog w.r.t. the parent anymore

XML_Editor.setWindowFlags(QtCore.Qt.Dialog or QtCore.Qt.WindowMaximizeButtonHint or QtCore.Qt.CustomizeWindowHint) -- no effect

Now my question is: How do I achieve that the Maximize button is shown and the dialog pops up centered w.r.t. the parent? Most resources on the web seem to focus on how to get rid of the Maximize button. Any ideas how to achieve the opposite?

The main target is Ubuntu 10.04 (default configuration), it would be great if it worked on Windows and Mac, too.

I appreciate any hint. Thanks in advance.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • Very similar to http://stackoverflow.com/questions/4699808/cant-add-minimize-button-to-qdialog-under-linux . No real solution there, too :-( – krlmlr Jan 23 '12 at 20:59

2 Answers2

6

The various window managers on the main platforms will all behave somewhat differently, so it will be hard to come up with a solution that is 100% guaranteed to work in all cases.

Using the default windowFlags on Linux KDE produces a dialog with context, maximize, minimize and close buttons; but with Windows XP, there's only a context and close button.

For most platforms, it would appear that at least the WindowSystemMenuHint and WindowMaximizeButtonHint flags need to be set in order to ensure the maximize button is present.

To ensure that the dialog stays centered on the parent window, just pass a reference to the parent in the dialog's constructor.

(Note that when combining the flags, you must use the bitwise OR operator |. In your examples, you use the boolean OR operator or, which would select the first non-zero value, but ignore the others).

The following example produces a centered dialog with maximize button for me on both Linux KDE and Windows XP:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Show Dialog', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        dialog = QtGui.QDialog(self)
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.setWindowFlags(dialog.windowFlags() |
                              QtCore.Qt.WindowSystemMenuHint |
                              QtCore.Qt.WindowMinMaxButtonsHint)
        dialog.resize(160, 120)
        dialog.show()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    window.resize(320, 240)
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks for the clarification concerning `|`, and also for the code. Unfortunately, still no success in my version of Unity. Seems to be dependent on the window manager, as stated in http://stackoverflow.com/questions/4699808/cant-add-minimize-button-to-qdialog-under-linux . It works reliably with `QtCore.Qt.Window` as window flag, so I guess I'll use this solution, given that my code base already contains a similar case. I'll accept your answer. – krlmlr Jan 23 '12 at 21:02
  • Replacing `dialog.windowFlags()` by `Qt.Window` works for me in Manjaro Cinnamon, in PyQt5. – Jason May 06 '19 at 02:58
0

Try to get the geometry of the parent window and then move the dialog to its center, something like:

QRect pw = parent_widget->getGeometry();
XML_Editor.move(pw.center() - XML_Editor->rect().center());
Gianluca
  • 3,227
  • 2
  • 34
  • 35
  • Thanks for the hint. While trying this, I realized that the parent widget is not the main window. – krlmlr Jan 23 '12 at 12:00