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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (add: Note: QLayout::setMargins was removed in qt6)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
[[Category:HowTo]]
{{LangSwitch}}
==Overview==
To adjust margins and spacing between {{DocLink|QWidget}}s use the following methods {{DocLink|QLayout|spacing-prop|setSpacing}} and {{DocLink|QLayout|contentsMargins|setContentsMargins}} that are implemented in class {{DocLink|QLayout}}.


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
==Example==
This code snippet shows how to remove spacing and margins between widgets in instance of {{DocLink|QVBoxLayout}}.


= Adjust Spacing and Margins between Widgets in Layout =
<pre>
pLayout = new QVBoxLayout(this);


== Overview ==
pLayout->setSpacing(0);
pLayout->setContentsMargins(0, 0, 0, 0);


To adjust margins and spacing between &quot;widgets&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html use the following methods &quot;setSpacing()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#spacing-prop and &quot;setContentsMargins()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#setContentsMargins that are implemented in class &quot;QLayout&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qlayout.html#details.
pLayout->addWidget(m_pLabel, 0, Qt::AlignTop);
pLayout->addWidget(m_pButton, 0, Qt::AlignTop);


== Example ==
setLayout(pLayout);
</pre>


This code snippet shows how to remove spacing and margins between widgets in instance of &quot;QVBoxLayout&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qvboxlayout.html
Note: <code>QLayout::setMargins</code> was removed in qt6
 
<code><br />pLayout = new QVBoxLayout(this);<br />pLayout-&gt;setSpacing(0);<br />pLayout-&gt;setMargin(0);<br />pLayout-&gt;setContentsMargins(0,0,0,0);<br />pLayout-&gt;addWidget(m_pLabel, 0, Qt::AlignTop);<br />pLayout-&gt;addWidget(m_pButton, 0, Qt::AlignTop);<br />setLayout(pLayout);<br /></code>

Latest revision as of 17:09, 27 January 2023

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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->setContentsMargins(0, 0, 0, 0);

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

setLayout(pLayout);

Note:

QLayout::setMargins

was removed in qt6