简体   繁体   English

消除QLabel顶部的间隙

[英]Removing gap from top of QLabel

I am showing image on my Qlabel. 我在我的Qlabel上显示图像。 Right now its having a gap between the top of the window and top of the QLabel(image) like this : 现在它在窗口顶部和QLabel(图像)顶部之间有一个间隙,如下所示:

在此输入图像描述

I don't want that gap, my UI current UI is like this : 我不希望这个差距,我的UI当前UI是这样的:

在此输入图像描述

Where label_image is the object name for my QLabel containing the image. 其中label_image是包含图像的QLabel的对象名称。

My constructor code : 我的构造函数代码:

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));
         }

}

Using the changes in centralWidget as shown I was able to get the image start from left most part, but am not able to get the image start from top. 如图所示,使用centralWidget的更改,我可以从最左边的部分开始获取图像,但是无法从顶部开始获取图像。

Edit : What I need is my image to start from the top left , left part is done, just the top part is left. 编辑:我需要的是我的图像从top left开始,左边部分完成,只剩下顶部。

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

要显示在顶部,请使用setAlignment()

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

Based on the comments and the xml file, I think a simple call to QBoxLayout::addStretch() right after you add label_image should suffice. 基于注释和xml文件,我认为在添加label_image之后立即调用QBoxLayout :: addStretch()就足够了。 This will insert a QSpacerItem in between the label and the horizontal layout that will keep the image on top, and the rest of the buttons at the bottom, even if you resize the widget. 这将在标签和水平布局之间插入一个QSpacerItem ,即使您调整小部件的大小,也会将图像保持在顶部,其余按钮位于底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM