ulak blade
ulak blade

Reputation: 2665

Qt stretching the parent window from the inside?

I have a window with a label in it in the middle, with other widgets above and below the label. I want to stretch the label to any height and thus, resizing the window and pushing the widgets away instead of intersecting them. Is there built-in functionality for this?

EDIT: I added a layout where all the widgets are, but when I expand one of them, it pushes the others into a corner without resizing the window.

Upvotes: 0

Views: 200

Answers (1)

Dennis
Dennis

Reputation: 186

You probably didn't add a layout. Here is an example with a QVBoxLayout which will place the widgets you'll add in it above eachother.

#include "class.h"
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>

Class::Class(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout* layout = new QVBoxLayout;

    QPushButton* button1 = new QPushButton("dummy1");
    QPushButton* button2 = new QPushButton("dummy2");
    QLabel* label = new QLabel("label");

    layout->addWidget(button1);
    layout->addWidget(label);
    layout->addWidget(button2);

    setLayout(layout);
}

Edit: if you really want to make your label push away everything you can add layout->addStretch(); above and under layout->addWidget(label); in the code

Upvotes: 1

Related Questions