QHBoxLayout中小部件之间的间距

Tca*_*chy 3 c++ qt qtgui qlabel

我正在尝试使用QtCreator创建GUI.对于此GUI,我需要显示彼此相邻的不同大小的多个图像.这些图像应该相互接触.

我用一个QWidget与一个QHBoxLayout,其中I添加标签(具有不同的大小)包含图像.

根据相关问题,我应该使用setSpacingsetContentsMargin删除这些空格,但这不起作用; 我试了好几次.

这是代码:

QWidget *widget = new QWidget(ui->tagcloud);
QHBoxLayout * l = new QHBoxLayout(widget);
ui->tagcloud->setWidget(widget);

for(int i=0;i<list.size();++i)
{
    QLabel *lab = new QLabel;

    QPixmap pic((list[i].imgPath).c_str());      //This fetches the image
    int sizeChange = 50 + (2*list[i].percent);   //Calculates the size of the image

    lab->setFixedSize(QSize(sizeChange, sizeChange));
    lab->setPixmap(pic);
    lab->setScaledContents(true);

    l->addWidget(lab);
    l->setSpacing(0);

}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,间距保持不变(即绝对不为零).如果我在布局中添加更多标签,间距似乎会变小.

任何人都可以解释或帮助我吗?谢谢!

Plu*_*luc 7

将间距设置为0并为我之前和之后添加拉伸:

l->addStretch();
for(int i = 0; i < list.size(); ++i)
{
    QLabel *lab = new QLabel;

    QPixmap pic((list[i].imgPath).c_str());      //This fetches the image
    int sizeChange = 50 + (2*list[i].percent);   //Calculates the size of the image

    lab->setFixedSize(QSize(sizeChange, sizeChange));
    lab->setPixmap(pic);
    lab->setScaledContents(true);

    l->addWidget(lab);
}
l->addStretch();

l->setSpacing(0);
Run Code Online (Sandbox Code Playgroud)

我认为这也是有效的

l->setSizeConstraint(QLayout::SetMaximumSize);
Run Code Online (Sandbox Code Playgroud)