<?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[How to add a paintEvent to a QWidget argument]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I have the below code :</p>
<p dir="auto">@void elevationForm:: setupUi(QWidget *Form)<br />
{<br />
if (Form-&gt;objectName().isEmpty())<br />
Form-&gt;setObjectName(QStringLiteral("Form"));<br />
Form-&gt;resize(231, 231);<br />
Form-&gt;setStyleSheet(QStringLiteral("background-image: url(:/pics/Elevation);"));</p>
<p dir="auto">}@</p>
<p dir="auto">Now I want to paint something in Form and to do that I need to implement the paintEvent in Form. Now how can I do that?<br />
I have already tried the below code and the compiler sent an error:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/form">@<bdi>Form-</bdi></a>&gt;paintEvent =this-&gt;drawline;<br />
Form-&gt;repaint();@</p>
<p dir="auto">Where drawline is:<br />
@void elevationForm::drawline(QPaintEvent *)<br />
{<br />
QPainter * P = new QPainter();<br />
QPen pen;<br />
pen.setBrush(Qt::red);<br />
P-&gt;setPen(pen);<br />
P-&gt;begin(this);<br />
P-&gt;drawLine(10,10,50,50);<br />
P-&gt;end();<br />
}@</p>
<p dir="auto">Thank you in advance</p>
]]></description><link>https://forum.qt.io/topic/33843/how-to-add-a-paintevent-to-a-qwidget-argument</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 12:54:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/33843.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 03 Nov 2013 06:42:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 11:03:22 GMT]]></title><description><![CDATA[<p dir="auto">Well, to be honest you can, but this is counter-intuitive and not really Qtish. I wouldn't recommend it but for the sake of completeness here's how to do it:</p>
<p dir="auto">@<br />
//this object will do the painting<br />
class EventHandler : public QObject {<br />
Q_OBJECT<br />
public:<br />
EventHandler(QObject* parent = 0) : QObject(parent) {}<br />
protected:<br />
bool eventFilter(QObject <em>obj, QEvent * event)<br />
{<br />
//handle only paint events<br />
//you should also check if obj is really a widget but I'll skip that<br />
if(event-&gt;type() == QEvent::Paint)<br />
{<br />
QPaintEvent</em> pevent = static_cast&lt;QPaintEvent*&gt;(event);<br />
QWidget* widget = static_cast&lt;QWidget*&gt;(obj);<br />
QPainter p(widget);<br />
//do something with p<br />
...<br />
//since we did all the drawing we return true<br />
//this prevents obj from receiving paintEvent<br />
return true;<br />
}<br />
return false; //ignore all other events<br />
}<br />
};</p>
<p dir="auto">//and then somewhere install the filter:<br />
myFormOrWhatever-&gt;installEventFilter(new EventHandler(parent));<br />
@</p>
]]></description><link>https://forum.qt.io/post/202744</link><guid isPermaLink="true">https://forum.qt.io/post/202744</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Mon, 04 Nov 2013 11:03:22 GMT</pubDate></item><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 11:01:39 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
Maybe install an eventfilter on the Form, there do the redraw, but subclassing the form widget is a better suggestion. Keeps it more object orientated.</p>
]]></description><link>https://forum.qt.io/post/202742</link><guid isPermaLink="true">https://forum.qt.io/post/202742</guid><dc:creator><![CDATA[Jeroentjehome]]></dc:creator><pubDate>Mon, 04 Nov 2013 11:01:39 GMT</pubDate></item><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 10:32:49 GMT]]></title><description><![CDATA[<p dir="auto">no... why can't you just subclass your form widget?</p>
]]></description><link>https://forum.qt.io/post/202738</link><guid isPermaLink="true">https://forum.qt.io/post/202738</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Mon, 04 Nov 2013 10:32:49 GMT</pubDate></item><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 10:27:06 GMT]]></title><description><![CDATA[<p dir="auto">Yes , as you can see from my code, I'm not trying to call the QpaintEvent of <code>Form</code> myself. I'm trying to implement it from outside of <code>Form</code>, and that is my question. Is there a way for me to implement the QpaintEvent function of <code>Form</code> from outside?</p>
]]></description><link>https://forum.qt.io/post/202736</link><guid isPermaLink="true">https://forum.qt.io/post/202736</guid><dc:creator><![CDATA[vahidnateghi]]></dc:creator><pubDate>Mon, 04 Nov 2013 10:27:06 GMT</pubDate></item><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 09:46:48 GMT]]></title><description><![CDATA[<p dir="auto">or when you need to outsource the implementation out of the paintEvent() handler you should pass a pointer to a QPainter instead.<br />
Then you can call this method from your paintEvent() handler and also from other methods which needs painting.</p>
<p dir="auto">But as Chris said, a method with a QPaintEvent* argument should never be called by yourself.</p>
]]></description><link>https://forum.qt.io/post/202728</link><guid isPermaLink="true">https://forum.qt.io/post/202728</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Mon, 04 Nov 2013 09:46:48 GMT</pubDate></item><item><title><![CDATA[Reply to How to add a paintEvent to a QWidget argument on Mon, 04 Nov 2013 09:32:07 GMT]]></title><description><![CDATA[<p dir="auto">You don't "call" paint event. It is called from your class whenever the widget needs to be redrawn.<br />
You should implement the paintEvent in your class, like so:</p>
<p dir="auto">@<br />
class elevationForm {<br />
...<br />
void paintEvent(QPaintEvent * e);<br />
}</p>
<p dir="auto">void elevationForm::paintEvent(QPaintEvent * e) {<br />
//call this if you need the default drawing first<br />
ParentOf ThisClass::paintEvent(e);</p>
<p dir="auto">//you've got leak in your code, just use stack variable<br />
//also painter needs to know where to draw (this)<br />
QPainter p(this);</p>
<p dir="auto">//do something with p<br />
...<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/202727</link><guid isPermaLink="true">https://forum.qt.io/post/202727</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Mon, 04 Nov 2013 09:32:07 GMT</pubDate></item></channel></rss>