How to set initial size of widget in PyQt larger than 2/3rds of screen

35,488

Solution 1

This should do it for you I think

from PyQt4 import QtCore, QtGui
import sys
w = 2280; h = 1520
app = QtGui.QApplication (sys.argv [1:])
frm = QtGui.QFrame ()
#frm.sizeHint = lambda: QtCore.QSize (w, h)
win = QtGui.QMainWindow ()
win.setCentralWidget (frm)
win.resize(w, h)
win.show ()
sys.exit (app.exec_ ())

Solution 2

The above answer is just fine, except that QApplication, QFrame and QMainWindow are not part of QtGui in the official PyQt5. They are part of QtWidgets.

from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame

w = 900; h = 600
app = QApplication (sys.argv [1:])
frm = QFrame ()
#frm.sizeHint = lambda: QtCore.QSize (w, h)
win = QMainWindow ()
win.setCentralWidget (frm)
win.resize(w, h)
win.show ()
sys.exit (app.exec_ ())
Share:
35,488
RolKau
Author by

RolKau

Updated on March 16, 2020

Comments

  • RolKau
    RolKau about 4 years

    In the main window I have a central widget which has a natural size, and I would like to initialize it to this size. However, I do not want it to be fixed to this size; the user should be able to shrink or expand it.

    The Qt documentation states that:

    Note: The size of top-level widgets are constrained to 2/3 of the desktop's height and width. You can resize() the widget manually if these bounds are inadequate.

    But I am unable to use the resize method as prescribed.

    The following minimal example illustrates the problem: If width and height as given by w and h is less than 2/3 of that of the screen, then the window gets the expected size. If they are greater, the window gets some truncated size.

    #!/usr/bin/env python
    from PyQt4 import QtCore, QtGui
    import sys
    w = 1280; h = 720
    app = QtGui.QApplication (sys.argv [1:])
    frm = QtGui.QFrame ()
    frm.sizeHint = lambda: QtCore.QSize (w, h)
    win = QtGui.QMainWindow ()
    win.setCentralWidget (frm)
    win.show ()
    sys.exit (app.exec_ ())
    
  • RolKau
    RolKau over 8 years
    This sets the client area of the window, not including the window manager's decorations, but if there are other widgets they are included in this size as well. For example, if I add the line win.statusBar ().showMessage ('Hello, World!'), the central widget has less than the specified size.
  • RolKau
    RolKau over 8 years
    I can of course do win.resize(w, h + win.statusBar().size ().height ()) to accomodate for all other widget I may have in the main window, but I wish for a less cumbersome solution.