简体   繁体   中英

Removing gap from top of QLabel

I am showing image on my Qlabel. Right now its having a gap between the top of the window and top of the QLabel(image) like this :

在此输入图像描述

I don't want that gap, my UI current UI is like this :

在此输入图像描述

Where label_image is the object name for my QLabel containing the image.

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.

Edit : What I need is my image to start from the top left , left part is done, just the top part is left.

My XML part of 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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