如何以包装的方式初始化布局,从而减少布局之间的空间

How to initialize layouts in a packed way, reducing space between layouts?

本文关键字:布局 之间 空间 包装 方式 初始化      更新时间:2023-10-16

我有几个qhboxlayout都包含在qvboxlayout中。

QHBoxLayout *hb1 = new QHBoxLayout;
// ...
QHBoxLayout *hb2 = new QHBoxLayout;
// ...
QWidget *hb1_layout = new QWidget;
hb1_layout->setLayout(hb1);
QWidget *hb2_layout = new QWidget;
hb2_layout->setLayout(hb2);
// ...
QVBoxLayout *ps = new QVBoxLayout;
ps->addWidget(hb1_layout);
ps->addWidget(hb2_layout);
ps->addWidget(hb3_layout);
ps->addWidget(hb4_layout);

我要做的是将小部件在每个qhboxlayout中打包在接近qhboxlayout下方的小部件的每个qhboxlayout中。即,水平布局之间每行小部件之间都有很多空间。

调整窗口的大小时,需要有一个最小尺寸的小部件不会超越。但是调整大小时,它们会均匀分布。

如果我调整了窗口大小,我可以将其看起来像我想要的。但是我不知道如何首先将窗口的编程初始化为正确的大小。当我启动应用程序时,我必须通过调整窗口大小以使小部件看起来很好地包装。

想法?

首先将窗口的编程初始化为正确的大小。

使用adjustSize();设置窗口尺寸适合您的小部件。

我要做的是在每个qhboxlayout中打包小部件 接近QHBoxLayout下一个下向下的小部件。

QVBoxLayout不会直接做到这一点。,而是使用 QFormLayout,行不会单独散开,行只扩展到其内容。您可以将行之间的间距调整为合适的int值,QFormLayout::setVerticalSpacing(40); ..和列之间QFormLayout::setHorizontalSpacing(40);

之间

,恕我直言,没有任何点操纵小部件的尺寸或每次窗口的尺寸都会调整大小。.如果您的窗口有少数小部件。.p>样本:

  QFormLayout * const formlayout = new QFormLayout(this->ui->centralWidget);
  QHBoxLayout *hb1 = new QHBoxLayout;
  QHBoxLayout *hb2 = new QHBoxLayout;
  QHBoxLayout *hb3 = new QHBoxLayout;
  QLabel *hb1_layout = new QLabel("Short Text1");
  hb1_layout->setStyleSheet("background-color: rgb(212, 192, 255)");
  hb1_layout->setFixedSize(100,100);
  hb1->addWidget(hb1_layout);
  formlayout->addRow(hb1);
  //
  QLabel *hb2_layout = new QLabel("Long Long Long Text");
  hb2_layout->setFixedSize(200,200);
  hb2_layout->setStyleSheet("background-color: rgb(25, 255, 192)");
  hb2->addWidget(hb2_layout);
  formlayout->addRow(hb2);
  //
  QLabel *hb3_layout = new QLabel("Long Long Long Text too n Long Long Long Text too n Long Long Long Text too");
  hb3_layout->setStyleSheet("background-color: rgb(100, 25, 25)");
  hb3_layout->setFixedSize(300,300);
  hb3->addWidget(hb3_layout);
  formlayout->addRow(hb3);
  //
  formlayout->setHorizontalSpacing(40); // between columns
  formlayout->setVerticalSpacing(10); // between rows
  adjustSize(); // Window to contents