0

I want to make a main window application in PyQt that its size is fixed.

I tried to find a way how to overload sizehint() method of the QMainWindow's layout in order to set the size returned by sizeHint() but didn't know how to do it.

After some researching i have learned that i can set the size of a QMainWindow with QWidget's function setFixedSize(QSize).

But still i'm curious how it can be done the other way.

It could be a lack of knowledge in Python programming.

Alon Lavi
  • 329
  • 2
  • 10

1 Answers1

1

Just for your curiosity you could do this:

from PyQt4 import QtGui as gui, QtCore as core

class MainWindow(gui.QMainWindow):

    def __init__(self):
        super(MainWindow,self).__init__()
        self.setCentralWidget(gui.QPushButton("Push me!"))

    def sizeHint(self):
        return core.QSize(300,500)


app = gui.QApplication([])


mw = MainWindow()
mw.show()

app.exec_()

You should keep in mind that sizeHint is only used to get a default size for the widgets when it is applicable. I mean when there is some extra space that could be filled or when there is more than one widget that share space in a layout, just experiment with it an layouts and you will see, could be very helpful! ;) Finally, it is not a valid approach for setting a fixed size, for that just use setFixedSize as you posted.

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68