[PyQt] question about size hints

Darren Dale dsdale24 at gmail.com
Sun May 2 13:05:54 BST 2010


On Sun, May 2, 2010 at 1:51 AM, Yao Ko <koyao at raptr.com> wrote:
> On Sat, May 1, 2010 at 5:47 AM, Darren Dale <dsdale24 at gmail.com> wrote:
>> Please excuse me for bumping. Does anyone have a suggestion?
>>
>> On Sun, Apr 18, 2010 at 12:42 PM, Darren Dale <dsdale24 at gmail.com> wrote:
>>> I have a question about size hints that can be illustrated with the
>>> simple example below. If I create my Test widget so it returns a size
>>> hint of (200,100), it is rendered with a size of 200, 100. If I create
>>> Test so it is 100x70, it is rendered to be 200x100. If I create test
>>> to be 1000x700, it is rendered to be 853x533 (my screen resolution is
>>> 1280x800). If I set the size policy to be fixed, then the central
>>> widget size policy is respected, but I can't resize the window. How
>>> can I make m respect the size hint of the central widget, but still
>>> maintain the ability to resize the window?
>>>
>>> Thank you,
>>> Darren
>>>
>>>
>>> import sys
>>> from PyQt4 import QtCore, QtGui
>>>
>>> class Test(QtGui.QWidget):
>>>
>>>    def __init__(self, width, height):
>>>        QtGui.QWidget.__init__(self)
>>>        #self.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
>>>        print 'Central widget should have width=%d, height=%d' %(width, height)
>>>        self._width = width
>>>        self._height = height
>>>
>>>    def sizeHint(self):
>>>        return QtCore.QSize(self._width, self._height)
>>>
>>> app = QtGui.QApplication([])
>>> m = QtGui.QMainWindow()
>>> c = Test(1000, 700)
>>> m.setCentralWidget(c)
>>> m.show()
>>> s = c.size()
>>> print 'but central widget has width=%d, height=%d'% (s.width(), s.height())
>>> sys.exit(app.exec_())
>
> Have you tried to set the size policy to:
>
> """
> ...
> class Test(QtGui.QWidget):
> ...
>       self.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding,
>                                  QtGui.QSizePolicy.MinimumExpanding)
> ...
> """
>
> The Test widget will have the minimum size specified by the sizeHint,
> and yet the MainWindow can still grow.

Thank you for the suggestion. I actually had tried that, but it is
unfortunately not an acceptable solution because I need to allow the
user to make the size smaller than the original specification. The
exact use case is defining a plot canvas in matplotlib.

Darren


More information about the PyQt mailing list