<?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 paint on a label created in QDialog class?]]></title><description><![CDATA[<p dir="auto">I have a lineEdit inside a label(the pink label in the picture). When the user types something inside that lineEdit, an arc must be drawn from 0 to 120 degree on the label. After searching about painting on label , I  found out that first I have to define a new class which inherits QLabel class and reimplement paintEvent inside that class (This solution is <a href="https://stackoverflow.com/questions/11973691/trying-to-paint-on-a-qlabel-fails-cannot-call-member-function-virtual-void-qla" target="_blank" rel="noopener noreferrer nofollow ugc">from here</a>).<br />
Therefore I added a new class named as "mylabel" which inherits QLabel and reimplemented QPaintevent inside that class, then inside the dialog class, I tried to draw that arc by typing inside the lineEdit, but nothing is drawn and I got error: painter not active.</p>
<p dir="auto">I would appreciate if someone could help me.</p>
<p dir="auto">Here is the code:</p>
<p dir="auto">mylabel.h:</p>
<pre><code>#ifndef MYLABEL_H
#define MYLABEL_H
#include &lt;QObject&gt;
#include &lt;QLabel&gt;

class MyLabel : public QLabel
{
    Q_OBJECT
    public:
        MyLabel(QWidget *parent = 0);
        void paintEvent(QPaintEvent *);
};


#endif

</code></pre>
<p dir="auto">mylabel.cpp:</p>
<pre><code>#include "mylabel.h"
#include &lt;QLabel&gt;

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{
    /*...*/


}

void MyLabel::paintEvent(QPaintEvent *)
{


}

</code></pre>
<p dir="auto">dialog.h:</p>
<pre><code>#ifndef DIALOG_H
#define DIALOG_H

#include "mylabel.h"
#include &lt;QDialog&gt;
#include &lt;QLineEdit&gt;
#include &lt;QVariantAnimation&gt;
#include &lt;QPainter&gt;
#include &lt;QLabel&gt;
#include &lt;QWidget&gt;

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

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

    QLineEdit *lineEdit_1;

    QLabel *label;

    int progress;





public slots:
    void setProgress();



private:
    Ui::Dialog *ui;


protected:
     void paintEvent(QPaintEvent *);




};

#endif // DIALOG_H

</code></pre>
<p dir="auto">dialog.cpp:</p>
<pre><code>#include "dialog.h"
#include "ui_dialog.h"
#include &lt;QGraphicsDropShadowEffect&gt;
#include &lt;QLabel&gt;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)

{
    ui-&gt;setupUi(this);

    label = new QLabel(this);
    label-&gt;setGeometry(50,50,600,500);
    label-&gt;show();
    label-&gt;setObjectName("label");
    label-&gt;setStyleSheet(
    "  QLabel#label{"
        "     background-color: pink;"
                "}"
        );

    lineEdit_1 = new QLineEdit(label);
    lineEdit_1-&gt;setGeometry(400,50,100,50);
    lineEdit_1-&gt;show();



    connect(lineEdit_1, SIGNAL(textChanged(const QString &amp;)), this, SLOT(setProgress()));


}

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

void Dialog::setProgress()
{

   progress = 120;
   this-&gt;update();
}


void Dialog::paintEvent(QPaintEvent *e)
{

    MyLabel mylabel;
    mylabel.paintEvent(e);
    QPainter p(label);


    QPen pen(QBrush("#00FF00"), 10);
    p.setPen(pen);
    p.setRenderHint(QPainter::Antialiasing);

    QRectF rectangle(100.0, 100.0, 100.0, 100.0);


   int startAngle = 0;
   int spanAngle = progress * 16;

   p.drawArc(rectangle, startAngle, spanAngle);
}



</code></pre>
<p dir="auto">main.cpp:</p>
<pre><code>#include "dialog.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

</code></pre>
<p dir="auto">my output:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/9a4ae43f-2c56-4858-9399-a6353efedeba.png" alt="myoutput.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/topic/122246/how-to-paint-on-a-label-created-in-qdialog-class</link><generator>RSS for Node</generator><lastBuildDate>Sat, 27 Jun 2026 22:58:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/122246.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 31 Dec 2020 14:01:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 10:02:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Hi. Yes you are right. Thank you so much. Really appreciate your help.</p>
]]></description><link>https://forum.qt.io/post/635225</link><guid isPermaLink="true">https://forum.qt.io/post/635225</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Fri, 01 Jan 2021 10:02:03 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 10:01:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you so much. As mylabel class inherits QLabel class, I had to set geometry and stylesheets to the label of mylabel class. Now my code works fine.</p>
]]></description><link>https://forum.qt.io/post/635224</link><guid isPermaLink="true">https://forum.qt.io/post/635224</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Fri, 01 Jan 2021 10:01:21 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 10:00:43 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
As <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> says<br />
you want</p>
<pre><code>
    mylabel = new MyLabel(this);
    mylabel -&gt;setGeometry(50,50,600,500);
    mylabel -&gt;show();
    mylabel -&gt;setObjectName("label");
    mylabel -&gt;setStyleSheet(
    "  QLabel#label{"
        "     background-color: pink;"
                "}"
        );
</code></pre>
<p dir="auto">so we use our custom one and not another plain one.</p>
]]></description><link>https://forum.qt.io/post/635223</link><guid isPermaLink="true">https://forum.qt.io/post/635223</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 01 Jan 2021 10:00:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 09:52:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> said in <a href="/post/635220">How to paint on a label created in QDialog class?</a>:</p>
<blockquote>
<pre><code>mylabel = new MyLabel(this);

label = new QLabel(this);
</code></pre>
</blockquote>
<p dir="auto">You have created a <code>MyLabel</code>, and then never used it?!  Instead retaining all your code which creates a <code>QLabel</code>??</p>
]]></description><link>https://forum.qt.io/post/635221</link><guid isPermaLink="true">https://forum.qt.io/post/635221</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 01 Jan 2021 09:52:16 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 09:46:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> I updated my code as you said. When I type in lineedit, nothing is drawn.</p>
<p dir="auto">dialog.h:</p>
<pre><code>#ifndef DIALOG_H
#define DIALOG_H

#include "mylabel.h"
#include &lt;QDialog&gt;
#include &lt;QLineEdit&gt;
#include &lt;QVariantAnimation&gt;
#include &lt;QPainter&gt;
#include &lt;QLabel&gt;
#include &lt;QWidget&gt;

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

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

    QLineEdit *lineEdit_1;

    QLabel *label;

    MyLabel *mylabel;


public slots:
    void setProgress();

private:
    Ui::Dialog *ui;


};

#endif // DIALOG_H

</code></pre>
<p dir="auto">dialog.cpp:</p>
<pre><code>#include "dialog.h"
#include "ui_dialog.h"
#include "mylabel.h"
#include &lt;QGraphicsDropShadowEffect&gt;
#include &lt;QLabel&gt;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)

{
    ui-&gt;setupUi(this);

    mylabel = new MyLabel(this);

    label = new QLabel(this);
    label-&gt;setGeometry(50,50,600,500);
    label-&gt;show();
    label-&gt;setObjectName("label");
    label-&gt;setStyleSheet(
    "  QLabel#label{"
        "     background-color: pink;"
                "}"
        );

    lineEdit_1 = new QLineEdit(label);
    lineEdit_1-&gt;setGeometry(400,50,100,50);
    lineEdit_1-&gt;show();



    connect(lineEdit_1, SIGNAL(textChanged(const QString &amp;)), this, SLOT(setProgress()));


}


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

void Dialog::setProgress()
{

    mylabel-&gt;SetProgress(120);
    mylabel-&gt;update();
}

</code></pre>
<p dir="auto">mylabel.h:</p>
<pre><code>#ifndef MYLABEL_H
#define MYLABEL_H
#include &lt;QObject&gt;
#include &lt;QLabel&gt;
#include "dialog.h"

class MyLabel : public QLabel
{
    Q_OBJECT
    public:
        MyLabel(QWidget *parent = 0);
        void paintEvent(QPaintEvent *);
        void SetProgress(int progressValue );
        int progress;

};


#endif

</code></pre>
<p dir="auto">mylabel.cpp:</p>
<pre><code>#include "mylabel.h"
#include &lt;QLabel&gt;

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{


}


void MyLabel::SetProgress(int progressValue )
{
   progress = progressValue;
   update();
}

void MyLabel::paintEvent(QPaintEvent *)
{


    QPainter p(this);

    QPen pen(QBrush("#00FF00"), 10);
    p.setPen(pen);
    p.setRenderHint(QPainter::Antialiasing);

    QRectF rectangle(100.0, 100.0, 100.0, 100.0);


   int startAngle = 0;
   int spanAngle = progress * 16;

   p.drawArc(rectangle, startAngle, spanAngle);
}

</code></pre>
]]></description><link>https://forum.qt.io/post/635220</link><guid isPermaLink="true">https://forum.qt.io/post/635220</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Fri, 01 Jan 2021 09:46:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 09:32:23 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
QPainter p(label);   should be<br />
QPainter p(this);</p>
<pre><code>void Dialog::setProgress()
{

   MyLabel mylabel;
   mylabel.SetProgress(120);
   this-&gt;update();
}

</code></pre>
<p dir="auto">this wont work as  MyLabel mylabel; will deleted very fast.</p>
<p dir="auto">You have to make it a member of Dialog<br />
so<br />
MyLabel *mylabel; in Dialog class  (in its h)</p>
<p dir="auto">then in Dialog constructor, you do once<br />
mylabel = new  MyLabel(this);</p>
<p dir="auto">then</p>
<pre><code>void Dialog::setProgress()
{  
   mylabel-&gt;SetProgress(120);
   mylabel-&gt;update();
}</code></pre>
]]></description><link>https://forum.qt.io/post/635216</link><guid isPermaLink="true">https://forum.qt.io/post/635216</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 01 Jan 2021 09:32:23 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Fri, 01 Jan 2021 09:25:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> said in <a href="/post/635148">How to paint on a label created in QDialog class?</a>:</p>
<blockquote>
<p dir="auto">mylabel-&gt;SetProgress(value from edit )</p>
</blockquote>
<p dir="auto">Hi<br />
Thank you for replying.<br />
I changed my code as you said. I got the error "label was not declared in this scope" (this error is related to the line : "QPainter p(label);" inside the paintevent in MyLabel class).<br />
As the label exists inside the window of QDialog class, how can I fix this error?</p>
<p dir="auto">Here is the code:</p>
<p dir="auto">mylabel.h:</p>
<pre><code>#ifndef MYLABEL_H
#define MYLABEL_H
#include &lt;QObject&gt;
#include &lt;QLabel&gt;
#include "dialog.h"

class MyLabel : public QLabel
{
    Q_OBJECT
    public:
        MyLabel(QWidget *parent = 0);
        void paintEvent(QPaintEvent *);
        void SetProgress(int progressValue );
        int progress;

};


#endif

</code></pre>
<p dir="auto">mylabel.cpp:</p>
<pre><code>#include "mylabel.h"
#include &lt;QLabel&gt;

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{


}


void MyLabel::SetProgress(int progressValue )
{
   progress = progressValue;
   update();
}

void MyLabel::paintEvent(QPaintEvent *)
{


    QPainter p(label);

    QPen pen(QBrush("#00FF00"), 10);
    p.setPen(pen);
    p.setRenderHint(QPainter::Antialiasing);

    QRectF rectangle(100.0, 100.0, 100.0, 100.0);


   int startAngle = 0;
   int spanAngle = progress * 16;

   p.drawArc(rectangle, startAngle, spanAngle);
}

</code></pre>
<p dir="auto">dialog.h:</p>
<pre><code>#ifndef DIALOG_H
#define DIALOG_H

#include "mylabel.h"
#include &lt;QDialog&gt;
#include &lt;QLineEdit&gt;
#include &lt;QVariantAnimation&gt;
#include &lt;QPainter&gt;
#include &lt;QLabel&gt;
#include &lt;QWidget&gt;

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

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

    QLineEdit *lineEdit_1;

    QLabel *label;

    int progress;



public slots:
    void setProgress();

private:
    Ui::Dialog *ui;


protected:
     void paintEvent(QPaintEvent *);


};

#endif // DIALOG_H
</code></pre>
<p dir="auto">dialog.cpp:</p>
<pre><code>#include "dialog.h"
#include "ui_dialog.h"
#include "mylabel.h"
#include &lt;QGraphicsDropShadowEffect&gt;
#include &lt;QLabel&gt;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)

{
    ui-&gt;setupUi(this);

    label = new QLabel(this);
    label-&gt;setGeometry(50,50,600,500);
    label-&gt;show();
    label-&gt;setObjectName("label");
    label-&gt;setStyleSheet(
    "  QLabel#label{"
        "     background-color: pink;"
                "}"
        );

    lineEdit_1 = new QLineEdit(label);
    lineEdit_1-&gt;setGeometry(400,50,100,50);
    lineEdit_1-&gt;show();



    connect(lineEdit_1, SIGNAL(textChanged(const QString &amp;)), this, SLOT(setProgress()));


}


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

void Dialog::setProgress()
{

   MyLabel mylabel;
   mylabel.SetProgress(120);
   this-&gt;update();
}

</code></pre>
<p dir="auto">main.cpp:</p>
<pre><code>#include "dialog.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

</code></pre>
]]></description><link>https://forum.qt.io/post/635215</link><guid isPermaLink="true">https://forum.qt.io/post/635215</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Fri, 01 Jan 2021 09:25:28 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Thu, 31 Dec 2020 14:37:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a><br />
Hi<br />
Move the paint code from<br />
void Dialog::paintEvent(QPaintEvent *e)<br />
to<br />
void MyLabel::paintEvent(QPaintEvent *)</p>
<p dir="auto">Then add a new function to MyLabel like<br />
MyLabel::SetProgress(int progressValue ) {<br />
progress = progressValue; // progress  is a new variable to put in MyLabel.h (in class) so PaintEvent can use it<br />
update(); // make label paint it self<br />
}</p>
<p dir="auto">Then in your slot setProgress you can</p>
<p dir="auto">mylabel-&gt;SetProgress(value from edit )<br />
and the label will redraw.</p>
]]></description><link>https://forum.qt.io/post/635148</link><guid isPermaLink="true">https://forum.qt.io/post/635148</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Thu, 31 Dec 2020 14:37:24 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Thu, 31 Dec 2020 14:21:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> Thank you for replying. Could you give suggestions for my case  (that I want to paint on the label which is inside QDialog window)? How can I draw on the pink label I posted?</p>
]]></description><link>https://forum.qt.io/post/635146</link><guid isPermaLink="true">https://forum.qt.io/post/635146</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Thu, 31 Dec 2020 14:21:27 GMT</pubDate></item><item><title><![CDATA[Reply to How to paint on a label created in QDialog class? on Thu, 31 Dec 2020 14:11:34 GMT]]></title><description><![CDATA[<p dir="auto">You can't call a widget's paintEvent inside another's, neither can you create a QPainter of a widget inside another's paintEvent.<br />
The point of creating a new class inherits QLabel is that you must do the drawing in its paintEvent.</p>
]]></description><link>https://forum.qt.io/post/635145</link><guid isPermaLink="true">https://forum.qt.io/post/635145</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Thu, 31 Dec 2020 14:11:34 GMT</pubDate></item></channel></rss>