【发布时间】: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。