如何减少Qt中布局内部小部件之间的差距

时间:2017-06-14 05:40:09

标签: c++ qt qt5 qlayout

我在运行时创建了一个VerticalLayout,并在其上添加了几个复选框。但是复选框在表单顶部有很大的空格。我无法缩小它们之间的差距并修剪顶部的额外空白。

以下是ui和cpp文件的源代码:

HideChartConfig.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>HideChartConfig</class>
 <widget class="QDialog" name="HideChartConfig">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>559</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string></string>
  </property>
  <widget class="QScrollArea" name="scrollArea">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>381</width>
     <height>471</height>
    </rect>
   </property>
   <property name="widgetResizable">
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="scrollAreaWidgetContents">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>379</width>
      <height>469</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QPushButton" name="okButton">
   <property name="geometry">
    <rect>
     <x>310</x>
     <y>510</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>OK</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

在HideChartConfig的构造函数中:

this->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    ui->setupUi(this);
    QPalette pal = ui->scrollArea->palette();
    pal.setColor(QPalette::Background, Qt::white);
    ui->scrollArea->setPalette(pal);

    QWidget* container = new QWidget();
    m_ContainerLayout = new QVBoxLayout();
    container->setLayout(m_ContainerLayout);
    ui->scrollArea->setWidget(container);

    m_ContainerLayout->addStretch();

    for (int i = 0; i < 3; i++)
    {
        QCheckBox *checkbox = new QCheckBox("Hello");
        m_ContainerLayout->addWidget(checkbox, 0, Qt::AlignTop);
    }


    m_ContainerLayout->addStretch();
    m_ContainerLayout->setSpacing(0);

还附上截图供参考:

click here to view the screenshot

我希望复选框显示在表单的顶部,并在顶部修剪额外的空白区域。

感谢任何帮助。提前谢谢!

3 个答案:

答案 0 :(得分:3)

您应该在布局容器的底部添加一个垂直QSpacerItem,并将其设置为Expanding,这将在布局底部使用尽可能多的空间,推动其上方的所有内容到顶部。

答案 1 :(得分:1)

尝试删除第一行

m_ContainerLayout->addStretch();

它提供了顶部的空间。 (第二行给出了底部的空间。)

答案 2 :(得分:1)

您不必创建新的QWidget,必须使用scrollAreaWidgetContents窗口小部件并将其设置为具有最小高度的大小策略。此外,您必须添加间隔符才能正确放置:

setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
ui->setupUi(this);
QPalette pal = ui->scrollArea->palette();
pal.setColor(QPalette::Background, Qt::white);
ui->scrollArea->setPalette(pal);

m_ContainerLayout = new QVBoxLayout(ui->scrollAreaWidgetContents);
ui->scrollArea->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

for (int i = 0; i < 3; i++)
{
    QCheckBox *checkbox = new QCheckBox("Hello");
    m_ContainerLayout->addWidget(checkbox);
}

<强> *。UI

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>HideChartConfig</class>
 <widget class="QDialog" name="HideChartConfig">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>559</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string/>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QScrollArea" name="scrollArea">
     <property name="widgetResizable">
      <bool>true</bool>
     </property>
     <widget class="QWidget" name="scrollAreaWidgetContents">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>380</width>
        <height>249</height>
       </rect>
      </property>
     </widget>
    </widget>
   </item>
   <item>
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>248</height>
      </size>
     </property>
    </spacer>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="okButton">
       <property name="text">
        <string>OK</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

截图:

enter image description here