- setSpacing(int)是設置layout中各部件的相鄰距離,如果不設置,這個距離默認是10。
- addSpacing(int)是在setSpacing(int)的基礎之上再插入間距。這個距離可以是負值,表示后一個部件會覆蓋在前一個部件上面。
QHBoxLayout *Layout = new QHBoxLayout(this); Layout->setSpacing(5); //設置各部件的相鄰距離 Layout->addSpacing(10); //插入間距10 Layout->addWidget(IconLabel); Layout->addSpacing(20); //插入間距20 Layout->addWidget(TitleLabel); Layout->addStretch(-10); //插入間距-10,使CloseButton部分覆蓋在TitleLabel上 Layout->addWidget(CloseButton); Layout->addStretch(); Layout->setContentsMargins(0, 0, 0, 0); //設置左上右下邊距 setLayout(Layout);
按照上面代碼設置后:
IconLabel和左邊界的距離為10 + 5 = 15;
TitleLabel和IconLabel的距離為20 + 5 = 25;
CloseButton和TitleLabel的距離為-10 + 5 = -5,意味着CloseButton有5像素寬的部分覆蓋在TitleLabel上。
————————————————
版權聲明:本文為CSDN博主「hAnjiaLE1998」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_43742643/article/details/100172184
