PDA

View Full Version : Default spacing in layouts



Caius Aérobus
7th December 2007, 18:17
Hello,
I have a big image on the left and a stack of thumbnails on the right. The size of the image on the left is fixed and I would like to compute the size (actually only height) of every thumbnail so as to have the top of the first thumbnail and the bottom of the last one aligned with the image top and bottom on the left.
So my computation is (left_height - (n-1) * right_spacing) / n but the spacing returned by the vertical layout is -1! So, as said in the doc, the spacing is those of the parent layout (no one here) or of the parent widget (ww here), right? And is it a (easy) way to get it, since layout->parent() returns a QObject and not ww (probably because I did not create it as a child of ww)? or an easier way to constraint the thumbnails sizes?



w = new QWidget();
QHBoxLayout *LLayout = new QHBoxLayout();
w->setLayout(LLayout);
LLayout->addWidget(<some widget>);
QWidget *ww = new QWidget();
QVBoxLayout layout = new QVBoxLayout();
layout->setMargin(1);
layout->setSizeConstraint(QLayout::SetFixedSize);
ww->setLayout(layout);
layout->addWidget(<some widget>);
layout->addWidget(<some widget>);
layout->addWidget(<some widget>);
LLayout->addWidget(ww);
LLayout->setAlignment(ww, Qt::AlignTop);

printf("spacing=%d\n", layout->spacing());

marcel
7th December 2007, 22:02
I will go ahead and assume the thumbnails are widgets.
It is just a matter of computing the spacing and thumbnail heights according to the height of the container widget.

Best fit for this task is a custom layout. Nothing fancy, just something that takes the parent height and lays out the layout items vertically. You can take a look at the layout example in Qt Demos. You can use that as model.

For alignment with the bigger image, it is as easy as adding the bigger image and thumbnail container in the same layout and setting equal margins to the inner layouts. That should do it.

Caius Aérobus
8th December 2007, 12:52
I will go ahead and assume the thumbnails are widgets.
It is just a matter of computing the spacing and thumbnail heights according to the height of the container widget.

Best fit for this task is a custom layout. Nothing fancy, just something that takes the parent height and lays out the layout items vertically. You can take a look at the layout example in Qt Demos. You can use that as model.

For alignment with the bigger image, it is as easy as adding the bigger image and thumbnail container in the same layout and setting equal margins to the inner layouts. That should do it.

Yes thumbnails are widgets.
Ok, I can use a custom layout but how could I get the spacing of the thumbnails layout?

Brandybuck
8th December 2007, 19:58
So my computation is (left_height - (n-1) * right_spacing) / n but the spacing returned by the vertical layout is -1!
Nothing to be surprised at. -1 indicates that the layout spacing is the default. This differs for each QStyle, and by widget type, and may even differ from direction to direction. If the spacing is -1, then you will have to query the style for the actual spacing: QStyle::layoutSpacing().