2012-08-17 2 views
12

상단에 버튼 목록을 포함하는 하나의 고정 된 높이와 위젯을 수직 및 수평으로 중심에 배치하는 레이아웃으로 잔재 공간을 채우는 하나의 레이아웃을 포함하는 Qt 윈도우를 만들고 싶습니다 아래 이미지를 참조하십시오. 고정 된 높이로 Qt 레이아웃 만들기

Example Qt layout

어떻게/위젯을 내 배치를 배치해야합니다. Ive는 중첩 된 가로 및 세로 레이아웃을 사용할 수있는 몇 가지 옵션을 시도했습니다

답변

15

분홍색 상자를 QHBoxLayout으로 만드는 것이 좋습니다 (단지 레이아웃이 아닌). 그 이유는 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);