[PyQt] Resizing window to smartly fit the contents

Matt Newell newellm at blur.com
Thu Nov 24 22:53:59 GMT 2011


On Thursday, November 24, 2011 09:49:51 AM Tom Bennett wrote:
> Hi Tayfun,
> 
> On Thu, Nov 24, 2011 at 2:26 AM, Tayfun Kayhan 
<tayfun92_kayhan at yahoo.com>wrote:
> > Let *self.win* be your widget, then *self.win.sizeHint()* returns the
> > ideal or recommended size of your widget.
> > *self.win.minimumSizeHint()* returns the minimum recommended size. you
> > can set these to resize your widget.
> > 
> >   ------------------------------
> > 
> > *From:* Tom Bennett <tom.bennett at mail.zyzhu.net>
> > *To:* pyqt at riverbankcomputing.com
> > *Sent:* Thursday, November 24, 2011 9:43 AM
> > *Subject:* [PyQt] Resizing window to smartly fit the contents
> > 
> > Hi,
> > 
> > I have a window that contains a composite widget that in turn contains
> > some basic widgets and a table view. Depending on different situations,
> > the table view can have a little or a lot data.
> > 
> > What I want is for the window to show a reasonable default size when it
> > comes up. In other words, when there is a little data, don't show a huge
> > window; when there is a lot of data, don't show a tiny window.
> > 
> > To make things more complicated, there has to be some minimum and maximum
> > size. For example, the window size cannot be bigger than the screen size.
> > But using the screen size is not exactly right, because the desktop
> > system occupies part of the screen. For example, on Windows, the bottom
> > area of the main screen is reserved for the system task bar, and should
> > not be covered by my window, whereas on the second screen usuallt one
> > can occupy the whole screen size.
> > 
> > I am wondering wat can be done to achieve this.
> > 
> > Thanks,
> > Tom
> > 
> > _______________________________________________
> > PyQt mailing list    PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
> This does not fully solve my problem. sizeHint() and minimumSizeHint() do
> not take into account how many contents are in QTableView.
> 
> Thanks,
> Tom

Subclass QTableView and provide a sizeHint() that takes into account the 
actual content.   All it takes is iterating through the rows and columns, 
though you'll probably want to break the loop at some point after getting to 
some maximum.

Matt


More information about the PyQt mailing list