简体   繁体   中英

How to create a compact Qt4 vBoxLayout

I've got a vBoxLayout which contains 3 simple buttons, when I increase the size of the widget containing the layout, the spacing between the buttons increases. I would like to stop this behaviour and keep the buttons in a consistent and compact layout, regardless of the size of the parent widget. This is what I've got so far, but it doesn't change the spacing, any suggestions?, thanks.

    button_layout = new QVBoxLayout ;
    button_layout -> setSpacing(0);
    button_layout -> setContentsMargins(0,0,0,0);

You'll want to add a stretchable spacer to the layout:

button_layout = new QVBoxLayout ;
button_layout -> setSpacing(0);
button_layout -> setContentsMargins(0,0,0,0);
button_layout -> addStretch();
button_layout -> addWidget(button_1);
button_layout -> addWidget(button_2);
button_layout -> addWidget(button_3);

This would cause the buttons to always be on the bottom of the containing widget. Note that the horizontal portion would still stretch if your widget expanded that way; to fix that, you need to either wrap in another (HBox) layout or switch to a grid layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM