2012-11-22 24 views
2

喜在中心顯示我使用QLabel來顯示QtForm的圖像。 我的代碼是這樣圖像不是QtForm

QVBoxLayout *layout = new QVBoxLayout; 
QHBoxLayout *hLayout = new QHBoxLayout; 
layout->setMargin(5); 
QLabel *imageLabel = new QLabel; 
QPixmap pixmap("/images/test.jpg"); 
imageLabel->setPixmap(pixmap); 
imageLabel->setMask(pixmap.mask()); 
imageLabel->setMinimumSize(160, 160); 
imageLabel->resize(500, 320); 

layout->addWidget(imageLabel,0,Qt::AlignTop | Qt::AlignCenter); 
hLayout->addItem(layout); 

widget->setLayout(hLayout); 

scrollArea->setWidget(widget); 
setCentralWidget(scrollArea); 

,但圖像的左上角顯示任何一個可以建議我帶來的圖像中心到形式

+0

發佈打印屏幕,所以我們可以看到它是怎麼樣的 – warunanc

回答

2

我得到了解決,

QPixmap pixmap("images/test.png"); 
imageLabel->setPixmap(pixmap); 
imageLabel->setMinimumSize(160, 160); 
imageLabel->resize(500, 320); 
imageLabel->setAlignment(Qt::AlignCenter); 


scrollArea->setWidget(widget); 
setCentralWidget(imageLabel); 
1

更改您的QLabel內容的對齊方式:

imageLabel->setAlignment(Qt::AlignCenter); 
+0

感謝您的建議尼康。它沒有工作:( –

0

如果setAlignment功能不起作用,然後與setSizePolicy一起使用。

imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 
imageLabel->setAlignment(Qt::AlignCenter); 
+1

請不要發佈裸露的代碼,也包括你在做什麼的解釋。 –