<?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[Performing QPainter outside paintEvent(QPaintEvent* event)]]></title><description><![CDATA[<p dir="auto">Hi everyone, I wanted to place the QPainter outside QPaintEvent because I want it to paint only once. If I were to place QPainter in the QPaintEvent, upon update(), I afraid it will continuously paint again and again when I place a variable inside paintEvent. This will ram up the CPU usage which is kind of unncessary.  Is there any way or example to paint outside QPaintEvent?</p>
<p dir="auto">I tried putting it in a an empty function but it doesnt work.<br />
Here is my code for reference. Thank you and looking forward for a discussion.</p>
<p dir="auto">//paint_circle.h</p>
<pre><code>//paint_circle.h
#ifndef PAINT_CIRCLE_H
#define PAINT_CIRCLE_H

#include &lt;QMainWindow&gt;
#include &lt;QPainter&gt;
#include &lt;QPaintEvent&gt;

namespace Ui {
class paint_circle;
}

class paint_circle : public QMainWindow
{
    Q_OBJECT

public:
    explicit paint_circle(QWidget *parent = 0);
    ~paint_circle();

    QSize sizeHint() const
    {
        return rect.size();
    }

protected:
    void paintEvent(QPaintEvent* event);

private:
    Ui::paint_circle *ui;
    QRect rect;

    void paint_test();

};

#endif // PAINT_CIRCLE_H

</code></pre>
<p dir="auto">//paint_circle.cpp</p>
<pre><code>//paint_circle.cpp
#include "paint_circle.h"
#include "ui_paint_circle.h"

paint_circle::paint_circle(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::paint_circle)
{
    ui-&gt;setupUi(this);
    rect = QRect(0, 0, this-&gt;width(), this-&gt;height());

    paint_test();
}

paint_circle::~paint_circle()
{
    delete ui;
}

void paint_circle::paint_test(){
    //I WANT TO TRANSFER LAYER 1 BACKGROUND DRAWING HERE.
}

void paint_circle::paintEvent(QPaintEvent* event)
{
    //BACKGROUND DRAWING (LAYER 1) 
    QRectF rectangle(0, 0, this-&gt;width(), this-&gt;height());
    QPainter b_color(this);
    b_color.setPen(QPen(Qt::white,0 ));
    b_color.fillRect(event-&gt;rect(), Qt::transparent);
    b_color.setBrush(QColor(Qt::red));
    b_color.drawRoundedRect(rectangle, 20.0, 10.0);

    //TO REMAIN HERE (LAYER 2)
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(QPen(Qt::white, 0));
    p.setBrush(Qt::black);
    p.drawEllipse(QRect((this-&gt;width()/2)-15,(this-&gt;height()/2)-15,30,30));
    p.rotate(90); // some random rotation number.
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/100002/performing-qpainter-outside-paintevent-qpaintevent-event</link><generator>RSS for Node</generator><lastBuildDate>Thu, 30 Apr 2026 03:02:29 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/100002.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Feb 2019 04:20:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Performing QPainter outside paintEvent(QPaintEvent* event) on Sun, 24 Feb 2019 09:57:09 GMT]]></title><description><![CDATA[<p dir="auto">If you must do something only once then that is a prime use of a static local variable:</p>
<pre><code>void method() {
    static bool first(true);
    if (first) { 
        do_stuff(); 
        first = false; 
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/513663</link><guid isPermaLink="true">https://forum.qt.io/post/513663</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Sun, 24 Feb 2019 09:57:09 GMT</pubDate></item><item><title><![CDATA[Reply to Performing QPainter outside paintEvent(QPaintEvent* event) on Sat, 23 Feb 2019 14:59:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Thank you!</p>
]]></description><link>https://forum.qt.io/post/513583</link><guid isPermaLink="true">https://forum.qt.io/post/513583</guid><dc:creator><![CDATA[Faruq]]></dc:creator><pubDate>Sat, 23 Feb 2019 14:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to Performing QPainter outside paintEvent(QPaintEvent* event) on Fri, 22 Feb 2019 05:43:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/faruq">@<bdi>Faruq</bdi></a> No, you can only paint on a widget inside paintEvent.<br />
Also what is the point to paint only once? After next update() your painting will be lost.<br />
If your painting is static you can paint onto a QPixmap once and then, in your paintEvent, you just paint that pixmap.</p>
]]></description><link>https://forum.qt.io/post/513234</link><guid isPermaLink="true">https://forum.qt.io/post/513234</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 22 Feb 2019 05:43:36 GMT</pubDate></item></channel></rss>