2012-08-17 85 views
12

我想創建一個Qt窗口,其中包含兩個佈局,一個固定高度,其中包含頂部按鈕列表,另一個填充重新渲染空間,並使用以垂直和水平方向居中控件的佈局根據下面的圖片。創建固定高度的Qt佈局

Example Qt layout

我應該如何鋪設了我的佈局/小部件。我嘗試了幾個選項,嵌套的水平和垂直佈局無效

回答

15

嘗試使用QHBoxLayout(而不是使其成爲佈局)使粉盒成爲QWidget。原因是QLayouts不提供固定大小的功能,但QWidgets可以。

// first create the four widgets at the top left, 
// and use QWidget::setFixedWidth() on each of them. 

// then set up the top widget (composed of the four smaller widgets): 
QWidget *topWidget = new QWidget; 
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget); 
topWidgetLayout->addWidget(widget1); 
topWidgetLayout->addWidget(widget2); 
topWidgetLayout->addWidget(widget3); 
topWidgetLayout->addWidget(widget4); 
topWidgetLayout->addStretch(1); // add the stretch 
topWidget->setFixedHeight(50); 

// now put the bottom (centered) widget into its own QHBoxLayout 
QHBoxLayout *hLayout = new QHBoxLayout; 
hLayout->addStretch(1); 
hLayout->addWidget(bottomWidget); 
hLayout->addStretch(1); 
bottomWidget->setFixedSize(QSize(50, 50)); 

// now use a QVBoxLayout to lay everything out 
QVBoxLayout *mainLayout = new QVBoxLayout; 
mainLayout->addWidget(topWidget); 
mainLayout->addStretch(1); 
mainLayout->addLayout(hLayout); 
mainLayout->addStretch(1); 

如果你真的想有兩個單獨的佈局 - 一個粉紅色的盒子,一個用於藍盒子 - 這個想法基本上是除了你同樣會做的藍色框變成自己QVBoxLayout,然後使用:

mainLayout->addWidget(topWidget); 
mainLayout->addLayout(bottomLayout); 
相關問題