1

我有一个包含 QVBoxLayout 的 QWidget,并且该布局包含 QLabel 和 QToolButtons。我的问题是,QLabel 占用了所有空间。我找到的唯一解决方案是将 maximumHeight 设置为 QLabel,但如果我这样做,Qt::AlignTop 将不再起作用。

主.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();
}

这张图片显示了 QLable 占用了多少空间(蓝色空间): QLabel_space

所以请帮助最小化 QLable 占用的空间:)

4

2 回答 2

1

您可以做的一种方法是为 QVBoxLayout 内的特定小部件设置拉伸因子。您可以在此处找到相关文档。

基本上,当您添加一个小部件时,您可以设置它,例如:

#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();
}
于 2013-08-30T21:08:55.507 回答
1

您的问题是工具按钮具有固定大小,因此在调整大小时,标签是唯一可以增长的类型:因此:添加标签后,在布局中添加拉伸:

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

修改后的代码 - 在底部添加拉伸。标签大小保持固定,按钮保留在其下方。为了测试,我已经移除了外面的整个主窗口。

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();
}
于 2013-08-30T21:16:25.170 回答