如何删除QLabel和QLineEdit之间的空格

时间:2013-01-16 22:14:26

标签: qt

我想减少标签和QLineEdit之间的空间(QLabel高于QLineEdit)。我怎样才能实现它?在我正在创建项目的代码中,我后来放入了一些布局。

QLabel* lgamma = new QLabel("Gamma");
gamma = new QLineEdit();
QLabel* lmin_linie = new QLabel(QString::fromUtf8("Min. il. zmian linii"));
min_lin = new QLineEdit();
// ...
QLabel* lmax_kursy = new QLabel(QString::fromUtf8("Max zm. il. kursów"));
max_kursy = new QLineEdit();
QGridLayout *lay = new QGridLayout(this);
QVBoxLayout *box1 = new QVBoxLayout();
QVBoxLayout *box2 = new QVBoxLayout();
// ...
QVBoxLayout *box12 = new QVBoxLayout();
box1->addWidget(lmin_linie);
box1->addWidget(min_lin);
box2->addWidget(lmax_lin);
box2->addWidget(max_lin);
// ...
box12->addWidget(literacje);
box12->addWidget(iteracje);
verticalColumn1->addLayout(box1);
verticalColumn1->addLayout(box2);
// ...
verticalColumn3->addLayout(box12);

start = new QPushButton("Start", this);
QHBoxLayout *corn = new QHBoxLayout();
corn->addLayout(verticalColumn1);
corn->addLayout(verticalColumn2);
corn->addLayout(verticalColumn3);
QVBoxLayout *rup = new QVBoxLayout();
rup->addLayout(corn);
rup->addWidget(start);

enter image description here

2 个答案:

答案 0 :(得分:5)

您只需在布局中添加间隔符即可。

QSpacerItem *spacer = new QSpacerItem(1, 50, QSizePolicy::Ignored, QSizePolicy::Expanding);
box1.addItem(spacer); 

根据您的需要调整参数或QSpacerItem,因为考试QSizePolicy::Preferred可能优于QSizePolicy::Expanding,并减少首选高度(第二个参数)。 为什么不使用简单的方法与Qt设计师一起使用GUI?

答案 1 :(得分:1)

您可以尝试设置QLabel和QLineEdit边框:

lmax_kursy->setStyleSheet("border-width:0px");
max_kursy->setStyleSheet("border-width:0px");

或在布局中设置间距。首先检查间距的当前值:

box1->spacing();

如果为0,请尝试将负值设置为-2:

box1->setSpacing(-2);       // or 0 or something else