2013-12-18 217 views
3

我正在嘗試使用QtCreator創建一個GUI。對於這個圖形用戶界面,我需要顯示幾張不同大小的圖像。這些圖像應該互相接觸。QHBoxLayout中的部件之間的間距

我使用QWidgetQHBoxLayout,其中我添加包含圖像的標籤(不同大小)。

根據相關問題,我應該使用setSpacingsetContentsMargin來刪除這些空格,但這不行;我試了幾次。

下面的代碼:

QWidget *widget = new QWidget(ui->tagcloud); 
QHBoxLayout * l = new QHBoxLayout(widget); 
ui->tagcloud->setWidget(widget); 

for(int i=0;i<list.size();++i) 
{ 
    QLabel *lab = new QLabel; 

    QPixmap pic((list[i].imgPath).c_str());  //This fetches the image 
    int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image 

    lab->setFixedSize(QSize(sizeChange, sizeChange)); 
    lab->setPixmap(pic); 
    lab->setScaledContents(true); 

    l->addWidget(lab); 
    l->setSpacing(0); 

} 

然而,當我運行此,間距保持不變(即絕對不是零)。 如果我在佈局中添加更多標籤,間距似乎變小。

任何人都可以解釋或幫助我嗎?謝謝!

+1

setSpacing(N)設定*最小*像素的數QBoxLayout必須放置在項目之間。但是,如果QBoxLayout有額外的空間來填充,它可以放置更多的像素比項目之間的像素。 (順便說一下,你只需要調用setSpacing(0)一次 - 在for循環的每次迭代中調用它都不會受到影響,但它也沒有任何區別) –

+0

感謝解釋,現在一切都變得有意義了:) – Tcanarchy

回答

7

設置間距爲0,工程前後分別增加拉伸對我來說:

l->addStretch(); 
for(int i = 0; i < list.size(); ++i) 
{ 
    QLabel *lab = new QLabel; 

    QPixmap pic((list[i].imgPath).c_str());  //This fetches the image 
    int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image 

    lab->setFixedSize(QSize(sizeChange, sizeChange)); 
    lab->setPixmap(pic); 
    lab->setScaledContents(true); 

    l->addWidget(lab); 
} 
l->addStretch(); 

l->setSpacing(0); 

而且這個作品我覺得

l->setSizeConstraint(QLayout::SetMaximumSize); 
+0

非常感謝! 這正是我需要的。 – Tcanarchy

+0

很高興它有幫助。我經常倒下來拉伸自己^^ – Pluc