Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qt网格布局集内容边距不工作_C++_Qt - Fatal编程技术网

C++ qt网格布局集内容边距不工作

C++ qt网格布局集内容边距不工作,c++,qt,C++,Qt,我想把一堆垂直布局放在一个网格布局中,这些垂直布局的数量是未定义的。在每个垂直布局中,我在标签中有一张照片,在照片下的另一个标签中有一个标题,我将此垂直布局插入网格布局: int j = 0; ui->profile_post_layout->setContentsMargins(0,0,0,0); for(int i = 0; i < posts.size(); i++) { QPixmap * pic = new QPixmap(QS

我想把一堆垂直布局放在一个网格布局中,这些垂直布局的数量是未定义的。在每个垂直布局中,我在标签中有一张照片,在照片下的另一个标签中有一个标题,我将此垂直布局插入网格布局:

int j = 0;
    ui->profile_post_layout->setContentsMargins(0,0,0,0);
    for(int i = 0; i < posts.size(); i++)
    {
        QPixmap * pic = new QPixmap(QString::fromStdString(url));
        QPixmap* mypix = new QPixmap(pic->scaled(QSize(50,50),  Qt::KeepAspectRatio));
        QLabel* photo = new QLabel;
        photo->setPixmap(*mypix);
        QLabel* label = new QLabel(QString::fromStdString(title));
        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(photo);
        layout->addWidget(label);
        layout->setContentsMargins(0,0,0,0);
        ui->profile_post_layout->addLayout(layout,j,i);
        if(i % 4 == 0)
            j++;
    }
intj=0;
ui->profile\u post\u layout->setContentsMargins(0,0,0);
对于(int i=0;iscaled(QSize(50,50),Qt::KeepAspectRatio));
QLabel*photo=新的QLabel;
照片->setPixmap(*mypix);
QLabel*label=新的QLabel(QString::fromStdString(title));
QVBoxLayout*布局=新的QVBoxLayout;
布局->添加小部件(照片);
布局->添加小部件(标签);
布局->设置内容边缘(0,0,0,0);
ui->profile\u post\u layout->addLayout(布局,j,i);
如果(i%4==0)
j++;
}
但结果是照片的标签距离太远,这意味着设置的内容边距不起作用。这是最新的。
如何处理这个问题?tnx.

如果问题是照片和标签之间的距离太远,这不是因为分配
setContentMargins(0,0,0,0)
,事实上这样做会使距离更远(删除边距)

使照片和标签在布局中紧凑的一种方法是,可以尝试在栅格布局的循环末端添加垂直间隔


一个建议是,如果你在布局问题上有类似的问题,你可以通过ui设计器创建一个简单的测试,并在编码中对你的实际布局进行实验,这有助于理解问题。

ui->profile\u post\u布局
a
QVBoxLayout
?@vahancho不,这是一个网格布局。这个“sdsad”在哪里来自哪里?@vahancho这是标题。我已将标签的文本设置为标题
QLabel*label=newqlabel(QString::fromStdString(title))Hm,但创建的标签数量与循环中的图像数量相同。在屏幕截图上,我看到网格中有20幅图像,但下面只有一个文本标签。这怎么会发生?