简体   繁体   中英

How to reduce distance between widgets and window size with Qt?

What I currently have:

当前窗口

What I want:

想要的窗口

For those unable to view the images; the widgets are spread out by some sort of margin between them. I would like to keep them as close as possible. How can I squeeze the widgets closer together?

I have already tried:
setFixedSize(sizeHint()); and setSizeConstraint(QLayout::SetFixedSize); on the main window, layouts, and widget object. Nothing seems to work.

As an extra, I would also appreciate this:
(having the label get even closer to the lineEdit)

I am using Windows and Qt 5.11.1, 64-bits.
The window constructor code:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
    widget = new QWidget();
    label = new QLabel(tr("Enter your name:"));
    nameLine = new QLineEdit;
    nameLine->setMinimumWidth(250);
    label->setBuddy(nameLine);

    okButton = new QPushButton (tr("Ok"));
    clearButton = new QPushButton (tr("Clear"));

    connect(okButton, SIGNAL(clicked()), this, SLOT(message()));
    connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));

    QGridLayout *grid = new QGridLayout;
    grid->addWidget(label,0,0);
    grid->addWidget(nameLine,1,0);
    grid->addWidget(okButton,0,1);
    grid->addWidget(clearButton,1,1);

    widget->setLayout(grid);

    setWindowTitle(tr("Leo v0.0"));
    setCentralWidget(widget);
}

A possible solution is to establish a QVBoxLayout with addStretch() :

QVBoxLayout *vlay = new QVBoxLayout;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label, 0, 0);
grid->addWidget(nameLine, 1, 0);
grid->addWidget(okButton, 0, 1);
grid->addWidget(clearButton, 1, 1);
vlay->addLayout(grid);
vlay->addStretch();
widget->setLayout(vlay);
setCentralWidget(widget);

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