Adjust Spacing and Margins between Widgets in Layout: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(Style)
Line 1: Line 1:
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:snippets]]
'''English''' | [[Adjust_Spacing_and_Margins_between_Widgets_in_Layout_German|Deutsch]]
[toc align_right="yes" depth="2"]
= Adjust Spacing and Margins between Widgets in Layout =
== Overview ==
== Overview ==
 
To adjust margins and spacing between {{DocLink|QWidget}}s use the following methods {{DocLinkAnchorLbl|QLayout|spacing-prop|setSpacing}} and {{DocLinkAnchorLbl|QLayout|contentsMargins|setContentsMargins}} that are implemented in class {{DocLink|QLayout}}.
To adjust margins and spacing between [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html widgets] use the following methods [http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#spacing-prop setSpacing()] and [http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#setContentsMargins setContentsMargins()] that are implemented in class [http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#details QLayout].


== Example ==
== Example ==
 
This code snippet shows how to remove spacing and margins between widgets in instance of {{DocLink|QVBoxLayout}}.
This code snippet shows how to remove spacing and margins between widgets in instance of [http://doc.qt.io/qt-5.0/qtwidgets/qvboxlayout.html QVBoxLayout]


<code>
<code>
pLayout = new QVBoxLayout(this);
pLayout = new QVBoxLayout(this);
pLayout->setSpacing(0);
pLayout->setSpacing(0);
pLayout->setMargin(0);
pLayout->setMargin(0);
pLayout->setContentsMargins(0,0,0,0);
pLayout->setContentsMargins(0,0,0,0);
pLayout->addWidget(m_pLabel, 0, Qt::AlignTop);
pLayout->addWidget(m_pLabel, 0, Qt::AlignTop);
pLayout->addWidget(m_pButton, 0, Qt::AlignTop);
pLayout->addWidget(m_pButton, 0, Qt::AlignTop);
setLayout(pLayout);
setLayout(pLayout);
</code>
</code>

Revision as of 19:54, 12 March 2015

Overview

To adjust margins and spacing between QWidgets use the following methods setSpacing and setContentsMargins that are implemented in class QLayout.

Example

This code snippet shows how to remove spacing and margins between widgets in instance of QVBoxLayout.

pLayout = new QVBoxLayout(this);

pLayout->setSpacing(0);
pLayout->setMargin(0);

pLayout->setContentsMargins(0,0,0,0);

pLayout->addWidget(m_pLabel, 0, Qt::AlignTop);
pLayout->addWidget(m_pButton, 0, Qt::AlignTop);

setLayout(pLayout);