2012-08-17 18 views
12

Voglio creare una finestra Qt che contiene due layout, un'altezza fissa che contiene un elenco di pulsanti nella parte superiore e uno che riempie lo spazio rimanente con un layout che centra verticalmente e orizzontalmente un widget come da immagine qui sotto.Crea layout Qt ad altezza fissa

Example Qt layout

come dovrei essere, che fissa i miei layout/widgets. Ho provato alcune opzioni con layout orizzontali e verticali nested senza alcun risultato

risposta

15

Provate a fare del QWidget un QWidget con un QHBoxLayout (invece di renderlo un layout). Il motivo è che QLayouts non fornisce funzionalità per creare dimensioni fisse, ma 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); 

Se si vuole veramente avere due layout separati - uno per la scatola rosa e uno per la scatola blu - l'idea è fondamentalmente la stessa, tranne che saresti la scatola blu nella propria QVBoxLayout, e quindi utilizzare:

mainLayout->addWidget(topWidget); 
mainLayout->addLayout(bottomLayout); 
Problemi correlati