【问题标题】:QLabel takes complete spaceQLabel 占用完整空间
【发布时间】:2013-09-03 16:21:07
【问题描述】:

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

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

这是一张显示 QLable 占用多少空间的图片(蓝色空间):

所以请帮助减少 QLable 占用的空间 :)

【问题讨论】:

  • stretch factor of the label 可以从QVBoxLayout 中设置。
  • 他已经说过为什么这样不行……
  • 拉伸因子没有帮助,无论我将因子设置为负值还是 null。

标签: c++ qt space qlabel


【解决方案1】:

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

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

【讨论】:

  • 很好,到目前为止可以,但是现在按钮停留在底部,如何更改按钮的对齐方式(没有 setAlign-function)?
  • 当您添加它们时,第三个参数是我回复中指示的对齐方式。顺便说一句,你确定你完全检查了我的回复吗?
  • 在最后一个按钮之后添加一些拉伸。
【解决方案2】:

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

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

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

【讨论】:

  • 这对我没有帮助。我试过“layout_steps->setStretchFactor(label_steps_start, -1);”以及“layout_steps->setStretchFactor(label_steps_start, 0);”。什么都没有改变...
  • 甚至不使用 aligntop?
  • 我刚刚将 strechFactor 添加到 addWidget-function 中,如您所示,并让其他代码保持不变 -> 没有发生任何事情
  • @user1655806:如果这个答案对您有帮助,请修改正确答案以供以后查看此线程的人使用,否则对其他回复表示感谢。 :)
猜你喜欢
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 2020-09-21
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多