Create Qt layout with fixed height

后端 未结 1 1511
一向
一向 2021-02-12 21:26

I want to create a Qt window that contains two layouts, one fixed height that contains a list of buttons at the top and one that fills the remaning space with a layout that cent

相关标签:
1条回答
  • 2021-02-12 22:10

    Try making the pink box a QWidget with a QHBoxLayout (rather than just making it a layout). The reason is that QLayouts don't provide functionality to make fixed sizes, but QWidgets do.

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

    If you really want to have two separate layouts--one for the pink box and one for the blue box--the idea is basically the same except you'd make the blue box into its own QVBoxLayout, and then use:

    mainLayout->addWidget(topWidget);
    mainLayout->addLayout(bottomLayout);
    
    0 讨论(0)
提交回复
热议问题