【问题标题】:Create Qt layout with fixed height创建具有固定高度的 Qt 布局
【发布时间】:2012-08-14 00:30:24
【问题描述】:

我想创建一个包含两种布局的 Qt 窗口,一种是固定高度,其中包含顶部的按钮列表,另一种是填充剩余空间的布局,该布局使小部件垂直和水平居中,如下图所示。

我应该如何布置我的布局/小部件。我尝试了一些嵌套水平和垂直布局的选项,但无济于事

【问题讨论】:

    标签: qt layout


    【解决方案1】:

    尝试使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2013-07-05
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多