krlmlr
krlmlr

Reputation: 25484

Add a Maximize button to a PyQt4 dialog and keep the dialog centered to the parent window

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.

Upvotes: 4

Views: 5363

Answers (2)

ekhumoro
ekhumoro

Reputation: 120798

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_())

Upvotes: 6

user1092803
user1092803

Reputation: 3297

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());

Upvotes: 0

Related Questions