سؤال

I have a QWidget which contains a QVBoxLayout and that layout contains a QLabel and QToolButtons. My problem is, that the QLabel takes all the space. The only solution I found is to set maximumHeight to the QLabel, but if I do that, the Qt::AlignTop doesn't work anymore.

main.cpp:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget window_main;

    QWidget *widget_steps = new QWidget(&window_main);
    widget_steps->setFixedWidth(75);
    widget_steps->move(QPoint(0, 0));
    widget_steps->setStyleSheet("background-color: red;");

    QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
    layout_steps->setContentsMargins(0, 0, 0, 0);
    layout_steps->setSpacing(0);

    QLabel *label_steps_start = new QLabel("steps:");
    label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    label_steps_start->setStyleSheet("background-color: blue;");
    layout_steps->addWidget(label_steps_start);

    QToolButton *tbutton_step1 = new QToolButton();
    layout_steps->addWidget(tbutton_step1);

    QToolButton *tbutton_step2 = new QToolButton();
    layout_steps->addWidget(tbutton_step2);

    QToolButton *tbutton_step3 = new QToolButton();
    layout_steps->addWidget(tbutton_step3);


    window_main.showMaximized();

    return a.exec();
}

Here a picture that shows how much space the QLable takes(the blue space): QLabel_space

So please help to minimize the space the QLable takes :)

هل كانت مفيدة؟

المحلول

Your problem is that the tool buttons have a fixed size, and therefore when resizing, the label is the only type that can grow: Therefore: After adding the label, add stretch to the layout:

layout_steps->addWidget(label_steps_start);
layout_steps->addStretch();

Modified code - adds stretch at the bottom. Label size remains fixed, and buttons remain under it. I've removed the whole main window around the outside for the sake of testing.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget *widget_steps = new QWidget;
    widget_steps->setFixedWidth(75);
    widget_steps->move(QPoint(0, 0));
    widget_steps->setStyleSheet("background-color: red;");

    QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
    layout_steps->setContentsMargins(0, 0, 0, 0);
    layout_steps->setSpacing(0);

    QLabel *label_steps_start = new QLabel("steps:");
    label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    label_steps_start->setStyleSheet("background-color: blue;");
    layout_steps->addWidget(label_steps_start);
    //--- Removed.... layout_steps->addStretch();

    QToolButton *tbutton_step1 = new QToolButton();
    layout_steps->addWidget(tbutton_step1);

    QToolButton *tbutton_step2 = new QToolButton();
    layout_steps->addWidget(tbutton_step2);

    QToolButton *tbutton_step3 = new QToolButton();
    layout_steps->addWidget(tbutton_step3);
    layout_steps->addStretch(); //<----- Added!
    widget_steps->show();

    return a.exec();
}

نصائح أخرى

One way you could do is to set the stretch factor for that particular widget inside the QVBoxLayout. You can find the documentation for that in here.

Basically, when you add a widget you can set that, for instance:

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget window_main;

    QWidget *widget_steps = new QWidget(&window_main);
    widget_steps->setFixedWidth(75);
    widget_steps->move(QPoint(0, 0));
    widget_steps->setStyleSheet("background-color: red;");

    QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
    layout_steps->setContentsMargins(0, 0, 0, 0);
    layout_steps->setSpacing(0);

    QLabel *label_steps_start = new QLabel("steps:");
    label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    label_steps_start->setStyleSheet("background-color: blue;");
    layout_steps->addWidget(label_steps_start, 3, Qt::AlignTop);
    layout_steps->addStretch();

    QToolButton *tbutton_step1 = new QToolButton();
    layout_steps->addWidget(tbutton_step1, 1);

    QToolButton *tbutton_step2 = new QToolButton();
    layout_steps->addWidget(tbutton_step2, 1);

    QToolButton *tbutton_step3 = new QToolButton();
    layout_steps->addWidget(tbutton_step3, 1);


    window_main.showMaximized();

    return a.exec();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top