Shortcut expands vertically in QVBoxLayout

I inserted a QLabelwith a small sentence a QLineEditand a QPushButtonin that order in QVBoxLayout. My main window is 70% of the user's desktop.

My problem is that my shortcut expands to almost 80% of the height of the parent window, QLineEditand `QButton \ are compressed at the bottom.

I figured out a way to solve this problem: I added more shortcuts without content, but this cannot be a golden solution. What can I do?

I also tried QFormLayout, but it does not fit my needs. I like the widgets are in vertical order. I tried many ways with QSizePolicy, but it did not work.

+5
source share
1 answer

, spacer. addStretch .

:

#include <QtGui>

class W: public QWidget
{
    Q_OBJECT

    public:
        W(bool spacer, QWidget *parent = 0)
            : QWidget(parent)
        {
            QLabel *l = new QLabel("Hello!");
            QLineEdit *e = new QLineEdit;
            QPushButton *p = new QPushButton("Go");

            QVBoxLayout *vl = new QVBoxLayout;
            vl->addWidget(l);
            vl->addWidget(e);
            vl->addWidget(p);

            if (spacer)
                vl->addStretch();

            setLayout(vl);

            resize(200, 400);
        }
};

:

enter image description here

( , .)

+7

All Articles