消除 QLabel 顶部的间隙

Removing gap from top of QLabel

我正在 Qlabel 上显示图片。现在它在 window 的顶部和 QLabel(image) 的顶部之间有一个间隙,如下所示:

我不想要那个差距,我的 UI 当前 UI 是这样的:

其中 label_image 是我的包含图像的 QLabel 的对象名称。

我的构造函数代码:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    this->setFixedSize(700,700);
    ui->setupUi(this);
    ui->centralWidget->layout()->setMargin(0);
    ui->centralWidget->layout()->setSpacing(0);

}

void MainWindow :: paintEvent(QPaintEvent * e)
{
    QMainWindow::paintEvent(e);
        if(1)
        {
           QImage image("/Users/arqam/Desktop/ImageToCartoon/input/ElonMusk.jpeg");
           //some redundant code
           ui->label_image->setPixmap(QPixmap::fromImage(image));
         }

}

如图所示,使用 centralWidget 中的更改我能够从最左侧开始获取图像,但无法从顶部开始获取图像。

编辑:我需要的是我的图像从 top left 开始,左边的部分已经完成,只剩下顶部。

我的 XML 部分 UI : https://pastebin.com/thxhc2Mj

要显示在顶部,请使用 setAlignment(),在您的情况下

ui->label_image->setAlignment(Qt::AlignTop);

根据评论和 xml 文件,我认为在标签和水平布局之间对 QBoxLayout::addStretch() right after you add label_image should suffice. This will insert a QSpacerItem 的简单调用将使图像保持在顶部,其余部分底部的按钮,即使您调整小部件的大小也是如此。