A
A
Alex Serov2017-05-05 16:52:45
Qt
Alex Serov, 2017-05-05 16:52:45

Why are widgets stacked on top of each other?

CocomoWidget::CocomoWidget(QWidget* parent) : QWidget(parent) {
    auto scaleLabel = new QLabel("Размер проекта:", this);

    auto scaleSpinBox = new QSpinBox(this);
    scaleSpinBox->setRange(1, 1000000);
    scaleSpinBox->setSingleStep(10);
    scaleSpinBox->setValue(10);
    scaleSpinBox->setSuffix(" KLOC");

    auto scaleLayout = new QHBoxLayout(this);
    scaleLayout->addWidget(scaleLabel);
    scaleLayout->addWidget(scaleSpinBox);

    auto topLayout = new QVBoxLayout(this);
    topLayout->addLayout(scaleLayout);

    this->setLayout(topLayout);
}

Result:
c8ee05926ee74bf392589cf8ea32556b.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2017-05-05
@gibsonman01

That's how it works. Look at the application console, it probably wrote "QLayout: Attempting to add QLayout "" to CustomWidget "", which already has a layout", because it was set when you specified this as a constructor parameter.

auto scaleLabel = new QLabel("Размер проекта:", this);

    auto scaleSpinBox = new QSpinBox(this);
    scaleSpinBox->setRange(1, 1000000);
    scaleSpinBox->setSingleStep(10);
    scaleSpinBox->setValue(10);
    scaleSpinBox->setSuffix(" KLOC");

    auto scaleLayout = new QHBoxLayout;
    scaleLayout->addWidget(scaleLabel);
    scaleLayout->addWidget(scaleSpinBox);

    auto topLayout = new QVBoxLayout;
    topLayout->addLayout(scaleLayout);

    this->setLayout(topLayout);

PS for widgets, you can also not add parent'a, tk. addWidget calls addItem which will add it.

I
Ighor July, 2017-05-05
@IGHOR

You only need one empty Layout in which to shove the rest of the Layout.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question