[英]Removing gap from top of QLabel
我在我的Qlabel上顯示圖像。 現在它在窗口頂部和QLabel(圖像)頂部之間有一個間隙,如下所示:
我不希望這個差距,我的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
開始,左邊部分完成,只剩下頂部。
我的UI部分: https : //pastebin.com/thxhc2Mj
要顯示在頂部,請使用setAlignment()
ui->label_image->setAlignment(Qt::AlignTop);
基於注釋和xml文件,我認為在添加label_image
之后立即調用QBoxLayout :: addStretch()就足夠了。 這將在標簽和水平布局之間插入一個QSpacerItem ,即使您調整小部件的大小,也會將圖像保持在頂部,其余按鈕位於底部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.