2012-08-17 35 views
12

Quiero crear una ventana de Qt que contiene dos diseños, uno de altura fija que contiene una lista de botones en la parte superior y uno que llena el espacio remaning con un diseño que se centra un widget vertical y horizontalmente según la imagen de abajo.Crear diseño Qt con altura fija

Example Qt layout

cómo debo ser trazar mis diseños/widgets. He intentado algunas opciones con diseños horizontales y verticales anidados en vano

Respuesta

15

Intente hacer del cuadro rosa un QWidget con un QHBoxLayout (en lugar de simplemente hacer un diseño). La razón es que QLayouts no proporciona funcionalidad para hacer tamaños fijos, pero los QWidgets sí.

// 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); 

Si realmente quiere tener dos diseños separados - uno para la caja de color rosa y una para el cuadro azul - la idea es básicamente el mismo, excepto que serías la caja azul en su propia QVBoxLayout, y luego usar:

mainLayout->addWidget(topWidget); 
mainLayout->addLayout(bottomLayout); 
Cuestiones relacionadas