<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[solved] placing QPushButtons inside a QVbox on mainwindow]]></title><description><![CDATA[<p dir="auto">I want to place some QPushButtons inside a QVboxLayout  from within code<br />
Mainwindow was created as mainwindow.ui using QT-Creator Designer.<br />
Following code creating a centerd pushbutton with full windowsize width regardless<br />
any geometry settings.<br />
I think i need to set "centralwidget" layout (?) but can not figure out how to do this.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/void">@<bdi>void</bdi></a> MainWindow::on_pb_test_2_clicked()<br />
{<br />
QVBoxLayout *vb = new QVBoxLayout(MainWindow::centralWidget());<br />
vb-&gt;setObjectName(QString("vb_1"));<br />
vb-&gt;setGeometry(QRect(100, 100,25,100));</p>
<pre><code>QPushButton *pb_1 = new QPushButton;
pb_1-&gt;setObjectName(QString("pb_1"));
pb_1-&gt;setStyleSheet(QString("QPushButton {background-color: red;}"));
pb_1-&gt;setGeometry(QRect(0, 0, 15, 15));
pb_1-&gt;setText("o");
vb-&gt;addWidget(pb_1);
</code></pre>
<p dir="auto">// ?  ui-&gt;centralWidget<br />
// ?  MainWindow-&gt;setCentralWidget(centralWidget());<br />
}@</p>
<p dir="auto">This code creates only a single pushbutton for now to keep things more clear.<br />
I found some 'solutions' on this forum but any of them fixes the problem for me.<br />
Thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/38282/solved-placing-qpushbuttons-inside-a-qvbox-on-mainwindow</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 09:49:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/38282.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Feb 2014 16:44:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Wed, 26 Feb 2014 06:24:47 GMT]]></title><description><![CDATA[<p dir="auto">Just QVBoxLayout is not sufficient, i need a QWidget too.<br />
(i guess the  QVBoxLayout is an abstract object only<br />
and needs a widget object to 'materialise' )<br />
Reduced to the essentials the following code works:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/void">@<bdi>void</bdi></a> MainWindow::on_pb_test_clicked()<br />
{<br />
QWidget *vLW = new QWidget(MainWindow::centralWidget());<br />
vLW-&gt;setObjectName(QString::fromUtf8("vLW"));<br />
vLW-&gt;setGeometry(QRect(150, 100, 31, 250));</p>
<pre><code>QVBoxLayout *vL = new QVBoxLayout(vLW);
vL-&gt;setObjectName(QString::fromUtf8("vL"));

QPushButton *pushButton = new QPushButton(vLW);
pushButton-&gt;setObjectName(QString::fromUtf8("pushButton"));
pushButton-&gt;setMaximumSize(QSize(20, 20));
pushButton-&gt;setStyleSheet(QString("QPushButton {background-color: red;}"));
vL-&gt;addWidget(pushButton);

vLW-&gt;show();
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/post/216211</link><guid isPermaLink="true">https://forum.qt.io/post/216211</guid><dc:creator><![CDATA[deleted28]]></dc:creator><pubDate>Wed, 26 Feb 2014 06:24:47 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Tue, 25 Feb 2014 20:47:57 GMT]]></title><description><![CDATA[<p dir="auto">Got it!<br />
tomorrow i 'll paste it here. Now i need a drink :)</p>
]]></description><link>https://forum.qt.io/post/216170</link><guid isPermaLink="true">https://forum.qt.io/post/216170</guid><dc:creator><![CDATA[deleted28]]></dc:creator><pubDate>Tue, 25 Feb 2014 20:47:57 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Tue, 25 Feb 2014 20:19:45 GMT]]></title><description><![CDATA[<p dir="auto">so i need yo add "spacers" in the manual code ?<br />
puh</p>
<p dir="auto">I just want the pushbutton stays inside the given geometry<br />
of the vboxlayout. I fear i need to read a lot more to get this work.</p>
]]></description><link>https://forum.qt.io/post/216168</link><guid isPermaLink="true">https://forum.qt.io/post/216168</guid><dc:creator><![CDATA[deleted28]]></dc:creator><pubDate>Tue, 25 Feb 2014 20:19:45 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Tue, 25 Feb 2014 20:06:24 GMT]]></title><description><![CDATA[<p dir="auto">Argh, I misread. So you have a pushbutton that hold the entire screen????<br />
That is what it suppose to do. Use the spacers to indicate to the layout that 'empty' space should be created if the pushbutton is beyond it's maximum size or geometry settings. Otherwise the Layout will force the pushbutton to take the entire screen.</p>
]]></description><link>https://forum.qt.io/post/216166</link><guid isPermaLink="true">https://forum.qt.io/post/216166</guid><dc:creator><![CDATA[Jeroentjehome]]></dc:creator><pubDate>Tue, 25 Feb 2014 20:06:24 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Tue, 25 Feb 2014 19:44:39 GMT]]></title><description><![CDATA[<p dir="auto">thanks for reply, but no change, still a full window width pushbutton, vertically centered on mainwindow.<br />
Built without any error.</p>
<p dir="auto">I added to position  line 13 in above code</p>
<p dir="auto">@ ui-&gt;centralWidget-&gt;setLayout(vb);@</p>
<p dir="auto">When i do the design i want using the designer i see in<br />
ui_mainwindow.h this lines:</p>
<p dir="auto">@verticalLayoutWidget = new QWidget(centralWidget);<br />
verticalLayoutWidget-&gt;setObjectName(QString::fromUtf8("verticalLayoutWidget"));<br />
verticalLayoutWidget-&gt;setGeometry(QRect(60, 70, 21, 261));</p>
<p dir="auto">verticalLayout = new QVBoxLayout(verticalLayoutWidget);<br />
verticalLayout-&gt;setSpacing(6);<br />
verticalLayout-&gt;setContentsMargins(11, 11, 11, 11);<br />
verticalLayout-&gt;setObjectName(QString::fromUtf8("verticalLayout"));<br />
verticalLayout-&gt;setContentsMargins(0, 0, 0, 0);@</p>
<p dir="auto">in the "manual" code i do not use anything similar to:<br />
@verticalLayoutWidget = new QWidget(centralWidget);@</p>
<p dir="auto">maybe this is my mistake ?</p>
]]></description><link>https://forum.qt.io/post/216163</link><guid isPermaLink="true">https://forum.qt.io/post/216163</guid><dc:creator><![CDATA[deleted28]]></dc:creator><pubDate>Tue, 25 Feb 2014 19:44:39 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] placing QPushButtons inside a QVbox on mainwindow on Tue, 25 Feb 2014 19:23:32 GMT]]></title><description><![CDATA[<p dir="auto">You probably need to set the layout of the central widget:<br />
@<br />
ui-&gt;centralWidget()-&gt;setLayout(&lt;your Vbox&gt;);<br />
@</p>
]]></description><link>https://forum.qt.io/post/216160</link><guid isPermaLink="true">https://forum.qt.io/post/216160</guid><dc:creator><![CDATA[Jeroentjehome]]></dc:creator><pubDate>Tue, 25 Feb 2014 19:23:32 GMT</pubDate></item></channel></rss>