2017-06-14 59 views
0

我在運行時創建了VerticalLayout並在其上添加了幾個複選框。但是複選框在表單頂部有巨大的空白區域。我無法縮小它們之間的差距,並在頂部修剪多餘的空白。如何減少Qt中的佈局內部的窗口小部件之間的差距

下面是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

我希望複選框出現在窗體的頂部,並在頂部修剪多餘的空白。

任何幫助表示讚賞。提前致謝!

+0

你可以表現出你的圖像想要得到。 – eyllanesc

+0

@eyllanesc:我希望複選框出現在窗體的頂部,並在頂部修剪額外的空白。 – gollusingh09

回答

1

嘗試刪除第一行

m_ContainerLayout->addStretch(); 

它詳細介紹了頂部的空間。 (第二行給出底部的空間。)

3

您應該將垂直QSpacerItem添加到佈局容器的底部,並將其設置爲Expanding,這將在佈局的底部使用盡可能多的空間,將其上方的所有空間推到頂部。

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

相關問題