| 方法 | 作用對象 | 視覺效果 | 典型值 |
|---|---|---|---|
setMargin(int) | 佈局邊緣與窗體/父控件的距離 | 四周留白(左、右、上、下) | 30 像素 |
setSpacing(int) | 佈局內相鄰控件之間的間距 | 控件之間的縱向/橫向間隙 | 40 像素 |
mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(30); // 控件與窗體邊界的邊距
mainLayout->setSpacing(40); // 控件之間的上下間距
+----------------------------------+ ← 窗體邊界 | margin=30px | | +--------------------------+ | | | Control 1 | | | +--------------------------+ | | spacing=40px | ← 控件間距 | +--------------------------+ | | | Control 2 | | | +--------------------------+ | | spacing=40px | | +--------------------------+ | | | Control 3 | | | +--------------------------+ | | margin=30px | +----------------------------------+
setMargin() 已棄用,建議使用 setContentsMargins(left, top, right, bottom) 獨立設置四邊本文整理自「大白菜」博客,原文地址:https://zhengyongkun.blog.51cto.com/1163218/1296980