<?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[Dynamically add Widgets to Layout]]></title><description><![CDATA[<p dir="auto">Ok i have the following Issue.</p>
<p dir="auto">I have a Main Window were i create a custom widget and set it as the Central Widget:</p>
<pre><code>MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    mMineField = new MineField(4, 4, 3, this);
    setCentralWidget(mMineField);
}
</code></pre>
<p dir="auto">The <code>MineField</code> creates depending of which width and height is supplied a count of <code>Cells</code> to display.</p>
<pre><code>#include &lt;QVector&gt;
#include &lt;QWidget&gt;

#include "cell.h"

class MineField : public QWidget
{
    Q_OBJECT
public:
    explicit MineField(int width,
                       int height,
                       int countOfBombs,
                       QWidget *parent = nullptr);

private:
    QVector&lt;Cell *&gt; createCells(int width, int height, int countOfBombs);

    int mWidth;
    int mHeight;
    QVector&lt;Cell *&gt; mCells;
};
</code></pre>
<p dir="auto">I try to create the Cells and add the to a grid layout like this:</p>
<pre><code>#include "minefield.h"

#include "cell.h"

#include &lt;QDebug&gt;
#include &lt;QGridLayout&gt;

#include &lt;random&gt;

MineField::MineField(int width,
                     int height,
                     int countOfBombs,
                     QWidget *parent)
    :QWidget{ parent },
      mWidth{ width},
      mHeight{ height },
      mCells{ createCells(width, height, countOfBombs) }
{
    auto layout = new QGridLayout;

    for(int i = 0; i &lt; mCells.size(); ++i) {
        auto row = static_cast&lt;int&gt;(i %  mWidth);
        auto column = static_cast&lt;int&gt;(i /  mWidth);

        qDebug() &lt;&lt; "Column = " &lt;&lt; column &lt;&lt; " Row = " &lt;&lt; row;

        layout-&gt;addWidget(mCells[i], row, column);
    }

    setLayout(layout);
}

QVector&lt;Cell *&gt; MineField::createCells(
        int width, int height, int countOfBombs)
{
    QVector&lt;Cell *&gt; cells;
    auto size = static_cast&lt;int&gt;(width * height);
    cells.reserve(size);

    for(auto i = 0; i &lt; size; ++i) {
        if(i &lt; countOfBombs) {
            cells.push_back(new Cell(Cell::State::mine, this));
        }
        else {
            cells.push_back(new Cell(Cell::State::empty, this));
        }
    }
    return cells;
}

</code></pre>
<p dir="auto">However i don't see anything displayed in the window. Is there an Issue in how i create the widgets dynamically?</p>
<p dir="auto">Edit:</p>
<p dir="auto">It looks like the <code>Cells</code> are at the correct position. Just the image of them is not displayed. I already tested that Cell paints itself directly in a QMainWindow. Then Why it does not work in the Layout?.</p>
<p dir="auto">The Cell  is also derrived from <code>QWidget</code> and paints like this:</p>
<pre><code>void Cell::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter{ this };  
    painter.drawImage(geometry(), displayImage(mDisplayType));
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/107382/dynamically-add-widgets-to-layout</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 12:55:55 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/107382.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Oct 2019 12:13:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamically add Widgets to Layout on Wed, 02 Oct 2019 14:47:26 GMT]]></title><description><![CDATA[<p dir="auto">yeah it was quite a nasty bug to find. Not very obvious in the first place.</p>
]]></description><link>https://forum.qt.io/post/554031</link><guid isPermaLink="true">https://forum.qt.io/post/554031</guid><dc:creator><![CDATA[sandro4912]]></dc:creator><pubDate>Wed, 02 Oct 2019 14:47:26 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add Widgets to Layout on Wed, 02 Oct 2019 12:51:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sandro4912">@<bdi>sandro4912</bdi></a><br />
Yep or else it might be offset in an unwanted way :)</p>
]]></description><link>https://forum.qt.io/post/553995</link><guid isPermaLink="true">https://forum.qt.io/post/553995</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 02 Oct 2019 12:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add Widgets to Layout on Wed, 02 Oct 2019 12:47:17 GMT]]></title><description><![CDATA[<p dir="auto">I found the Issue myself in the paintEvent of Cell.</p>
<p dir="auto">It has to use <code>rect()</code> instead of <code>geometry()</code>:</p>
<pre><code>void Cell::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter{ this };  
    painter.drawImage(rect(), displayImage(mDisplayType));
}
</code></pre>
]]></description><link>https://forum.qt.io/post/553992</link><guid isPermaLink="true">https://forum.qt.io/post/553992</guid><dc:creator><![CDATA[sandro4912]]></dc:creator><pubDate>Wed, 02 Oct 2019 12:47:17 GMT</pubDate></item></channel></rss>