13

I would like to know how to disable the window Maximise button in pyqt4. I am at present using QWidget.setFixedSize (self, QSize) to prevent user window resizing, however the maximise button is still enabled and when pressed causes the application to move to the top left corner of the screen. I am basically wanting to replicate the behaviour of the Windows calculator application, where the maximise icon is greyed out. Does anyone know how to achieve this with PyQt4?

jim
  • 331
  • 1
  • 3
  • 7

5 Answers5

17

Haven't worked with it but research seems to point to messing with the window flags.

QWidget has a method called setWindowFlags.

Here is the doc for the Qt.WindowFlags class.

Here is a reference for all of the flags. Look for Qt.WindowMaximizeButtonHint

In general it seems like you need to find a way to enable the Qt.CustomizeWindowHint flag and disable the Qt.WindowMaximizeButtonHint flag. Either way, you probably want this in addition to setFixedSize so that's a good start.

Edit:

Something like

win.setWindowFlags(win.windowFlags() | QtCore.Qt.CustomizeWindowHint)
win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)

Assuming your import is something like this

from PyQt4 import QtCore

This would turn on the CustomizeWindowHint flag and turn off the WindowMaximizeButtonHint flag, I hope. Let me know if this works at all.

Edit:

As discovered by OP, the only call necessary for his desired outcome:

win.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)

but beware, since this will also remove the close button and potentially mess with other window flags.

Brian
  • 3,091
  • 16
  • 29
  • Thanks Brian, your research pointed me in the right way. In the end it was simpler than you were thinking. A call to win.setWindowFlags was required however win.setWindowFlags(Qt.WindowMinimizeButtonHint) did the job. It appears a call to WindowMinimizeButtonHint will disable the window maximize button; and likewise a call to WindowMaximizeButton hint will disable the minimize button – jim Sep 04 '13 at 09:39
  • @jim Glad it helped! Good to know if I decide to start playing with pyqt in the future. – Brian Sep 04 '13 at 14:26
  • 1
    Note the original answer was the best one, since the one the OP chose also removes the close button and potentially changes the other window flags. – Siwel Nov 06 '17 at 23:07
15

This works perfectly:

MainWindow.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)
RPL
  • 3,500
  • 2
  • 21
  • 28
tech kamar
  • 151
  • 1
  • 2
1

you could set the maximumSize and minimumSize with the same values, it'll get to dissapear maximise button

GSandro_Strongs
  • 773
  • 3
  • 11
  • 24
1

This helped me:

Form.setMaximumSize(QtCore.QSize(width, height))

It would go here in your class:

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(328, 212)
        Form.setMaximumSize(QtCore.QSize(352, 189))
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Saa Gar
  • 11
  • 1
1

This works in PyQT6:

MainWindow.setWindowFlags(QtCore.Qt.WindowType.CustomizeWindowHint | QtCore.Qt.WindowType.WindowCloseButtonHint | QtCore.Qt.WindowType.WindowMinimizeButtonHint)

Hope it's useful!

Clayton Maciel
  • 161
  • 1
  • 4