PyQt layout question: QScrollView and QGridLayout?

David Boddie davidb at mcs.st-and.ac.uk
Thu Nov 17 18:43:52 EST 2005


Volker Lenhardt wrote:
> Phil Thompson schrieb:
> > On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:

[Using a QGridLayout in a QScrollView]

> >>Is there a way to get it to work? Filling a box viewport with lots of
> >>padding boxes and white space labels to establish grids is very
> >>cumbersome. And I need 4 different layouts to change places.
> >
> > QGridLayout is not a sub-class of QWidget, which is what addChild() is
> > expecting. You probably want QGrid.
>
> I hoped to find a more assuring answer. There's no MultiCellWidget, no
> Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
> VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
> white space QLabels to fill not used grid cells.

You can still use QGridLayout, but you need to put a widget into the
QScrollView in what the documentation describes as "Using One Big
Widget" (http://doc.trolltech.com/3.3/qscrollview.html).

Something along these lines should do basically what you want:

  sv = QScrollView()
  w = QWidget(sv.viewport())
  sv.addChild(w)

  grid = QGridLayout(w, 3, 3)

  for i in range(0,3):
    for j in range(0,3):
       grid.addWidget(QLabel("(%i,%i)" % (i,j), w), i, j)

Of course, the widget that contains the layout is only as large as it
needs to be, so you may need to call its setMinimumSize() method if
you want it to be larger. However, it's often a better idea to ensure
that the child widgets have the sizes they need - their parent will
then be as large as they require.

Hope this helps,

David




More information about the Python-list mailing list