RolKau
RolKau

Reputation: 1822

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

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

Upvotes: 6

Views: 48468

Answers (2)

Whitequill Riclo
Whitequill Riclo

Reputation: 840

The above answer is just fine, except that QApplication, QFrame and QMainWindow are not part of QtGui in the official PyQt5 package. 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_ ())

Upvotes: 6

Achayan
Achayan

Reputation: 5895

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

Upvotes: 10

Related Questions