Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to paint on a label created in QDialog class?
Qt 6.11 is out! See what's new in the release blog

How to paint on a label created in QDialog class?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nanor
    wrote on last edited by
    #1

    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 from here).
    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.

    I would appreciate if someone could help me.

    Here is the code:

    mylabel.h:

    #ifndef MYLABEL_H
    #define MYLABEL_H
    #include <QObject>
    #include <QLabel>
    
    class MyLabel : public QLabel
    {
        Q_OBJECT
        public:
            MyLabel(QWidget *parent = 0);
            void paintEvent(QPaintEvent *);
    };
    
    
    #endif
    
    

    mylabel.cpp:

    #include "mylabel.h"
    #include <QLabel>
    
    MyLabel::MyLabel(QWidget *parent)
        : QLabel(parent)
    {
        /*...*/
    
    
    }
    
    void MyLabel::paintEvent(QPaintEvent *)
    {
    
    
    }
    
    

    dialog.h:

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include "mylabel.h"
    #include <QDialog>
    #include <QLineEdit>
    #include <QVariantAnimation>
    #include <QPainter>
    #include <QLabel>
    #include <QWidget>
    
    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
    
    

    dialog.cpp:

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QGraphicsDropShadowEffect>
    #include <QLabel>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    
    {
        ui->setupUi(this);
    
        label = new QLabel(this);
        label->setGeometry(50,50,600,500);
        label->show();
        label->setObjectName("label");
        label->setStyleSheet(
        "  QLabel#label{"
            "     background-color: pink;"
                    "}"
            );
    
        lineEdit_1 = new QLineEdit(label);
        lineEdit_1->setGeometry(400,50,100,50);
        lineEdit_1->show();
    
    
    
        connect(lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(setProgress()));
    
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::setProgress()
    {
    
       progress = 120;
       this->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);
    }
    
    
    
    

    main.cpp:

    #include "dialog.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        return a.exec();
    }
    
    

    my output:

    myoutput.png

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      You can't call a widget's paintEvent inside another's, neither can you create a QPainter of a widget inside another's paintEvent.
      The point of creating a new class inherits QLabel is that you must do the drawing in its paintEvent.

      N 1 Reply Last reply
      2
      • B Bonnie

        You can't call a widget's paintEvent inside another's, neither can you create a QPainter of a widget inside another's paintEvent.
        The point of creating a new class inherits QLabel is that you must do the drawing in its paintEvent.

        N Offline
        N Offline
        nanor
        wrote on last edited by
        #3

        @Bonnie 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?

        mrjjM 1 Reply Last reply
        0
        • N nanor

          @Bonnie 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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nanor
          Hi
          Move the paint code from
          void Dialog::paintEvent(QPaintEvent *e)
          to
          void MyLabel::paintEvent(QPaintEvent *)

          Then add a new function to MyLabel like
          MyLabel::SetProgress(int progressValue ) {
          progress = progressValue; // progress is a new variable to put in MyLabel.h (in class) so PaintEvent can use it
          update(); // make label paint it self
          }

          Then in your slot setProgress you can

          mylabel->SetProgress(value from edit )
          and the label will redraw.

          N 1 Reply Last reply
          1
          • mrjjM mrjj

            @nanor
            Hi
            Move the paint code from
            void Dialog::paintEvent(QPaintEvent *e)
            to
            void MyLabel::paintEvent(QPaintEvent *)

            Then add a new function to MyLabel like
            MyLabel::SetProgress(int progressValue ) {
            progress = progressValue; // progress is a new variable to put in MyLabel.h (in class) so PaintEvent can use it
            update(); // make label paint it self
            }

            Then in your slot setProgress you can

            mylabel->SetProgress(value from edit )
            and the label will redraw.

            N Offline
            N Offline
            nanor
            wrote on last edited by
            #5

            @mrjj said in How to paint on a label created in QDialog class?:

            mylabel->SetProgress(value from edit )

            Hi
            Thank you for replying.
            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).
            As the label exists inside the window of QDialog class, how can I fix this error?

            Here is the code:

            mylabel.h:

            #ifndef MYLABEL_H
            #define MYLABEL_H
            #include <QObject>
            #include <QLabel>
            #include "dialog.h"
            
            class MyLabel : public QLabel
            {
                Q_OBJECT
                public:
                    MyLabel(QWidget *parent = 0);
                    void paintEvent(QPaintEvent *);
                    void SetProgress(int progressValue );
                    int progress;
            
            };
            
            
            #endif
            
            

            mylabel.cpp:

            #include "mylabel.h"
            #include <QLabel>
            
            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);
            }
            
            

            dialog.h:

            #ifndef DIALOG_H
            #define DIALOG_H
            
            #include "mylabel.h"
            #include <QDialog>
            #include <QLineEdit>
            #include <QVariantAnimation>
            #include <QPainter>
            #include <QLabel>
            #include <QWidget>
            
            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
            

            dialog.cpp:

            #include "dialog.h"
            #include "ui_dialog.h"
            #include "mylabel.h"
            #include <QGraphicsDropShadowEffect>
            #include <QLabel>
            
            Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
            
            {
                ui->setupUi(this);
            
                label = new QLabel(this);
                label->setGeometry(50,50,600,500);
                label->show();
                label->setObjectName("label");
                label->setStyleSheet(
                "  QLabel#label{"
                    "     background-color: pink;"
                            "}"
                    );
            
                lineEdit_1 = new QLineEdit(label);
                lineEdit_1->setGeometry(400,50,100,50);
                lineEdit_1->show();
            
            
            
                connect(lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(setProgress()));
            
            
            }
            
            
            Dialog::~Dialog()
            {
                delete ui;
            }
            
            void Dialog::setProgress()
            {
            
               MyLabel mylabel;
               mylabel.SetProgress(120);
               this->update();
            }
            
            

            main.cpp:

            #include "dialog.h"
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                Dialog w;
                w.show();
            
                return a.exec();
            }
            
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              QPainter p(label); should be
              QPainter p(this);

              void Dialog::setProgress()
              {
              
                 MyLabel mylabel;
                 mylabel.SetProgress(120);
                 this->update();
              }
              
              

              this wont work as MyLabel mylabel; will deleted very fast.

              You have to make it a member of Dialog
              so
              MyLabel *mylabel; in Dialog class (in its h)

              then in Dialog constructor, you do once
              mylabel = new MyLabel(this);

              then

              void Dialog::setProgress()
              {  
                 mylabel->SetProgress(120);
                 mylabel->update();
              }
              N 1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                QPainter p(label); should be
                QPainter p(this);

                void Dialog::setProgress()
                {
                
                   MyLabel mylabel;
                   mylabel.SetProgress(120);
                   this->update();
                }
                
                

                this wont work as MyLabel mylabel; will deleted very fast.

                You have to make it a member of Dialog
                so
                MyLabel *mylabel; in Dialog class (in its h)

                then in Dialog constructor, you do once
                mylabel = new MyLabel(this);

                then

                void Dialog::setProgress()
                {  
                   mylabel->SetProgress(120);
                   mylabel->update();
                }
                N Offline
                N Offline
                nanor
                wrote on last edited by
                #7

                @mrjj I updated my code as you said. When I type in lineedit, nothing is drawn.

                dialog.h:

                #ifndef DIALOG_H
                #define DIALOG_H
                
                #include "mylabel.h"
                #include <QDialog>
                #include <QLineEdit>
                #include <QVariantAnimation>
                #include <QPainter>
                #include <QLabel>
                #include <QWidget>
                
                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
                
                

                dialog.cpp:

                #include "dialog.h"
                #include "ui_dialog.h"
                #include "mylabel.h"
                #include <QGraphicsDropShadowEffect>
                #include <QLabel>
                
                Dialog::Dialog(QWidget *parent) :
                    QDialog(parent),
                    ui(new Ui::Dialog)
                
                {
                    ui->setupUi(this);
                
                    mylabel = new MyLabel(this);
                
                    label = new QLabel(this);
                    label->setGeometry(50,50,600,500);
                    label->show();
                    label->setObjectName("label");
                    label->setStyleSheet(
                    "  QLabel#label{"
                        "     background-color: pink;"
                                "}"
                        );
                
                    lineEdit_1 = new QLineEdit(label);
                    lineEdit_1->setGeometry(400,50,100,50);
                    lineEdit_1->show();
                
                
                
                    connect(lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(setProgress()));
                
                
                }
                
                
                Dialog::~Dialog()
                {
                    delete ui;
                }
                
                void Dialog::setProgress()
                {
                
                    mylabel->SetProgress(120);
                    mylabel->update();
                }
                
                

                mylabel.h:

                #ifndef MYLABEL_H
                #define MYLABEL_H
                #include <QObject>
                #include <QLabel>
                #include "dialog.h"
                
                class MyLabel : public QLabel
                {
                    Q_OBJECT
                    public:
                        MyLabel(QWidget *parent = 0);
                        void paintEvent(QPaintEvent *);
                        void SetProgress(int progressValue );
                        int progress;
                
                };
                
                
                #endif
                
                

                mylabel.cpp:

                #include "mylabel.h"
                #include <QLabel>
                
                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);
                }
                
                
                JonBJ 1 Reply Last reply
                0
                • N nanor

                  @mrjj I updated my code as you said. When I type in lineedit, nothing is drawn.

                  dialog.h:

                  #ifndef DIALOG_H
                  #define DIALOG_H
                  
                  #include "mylabel.h"
                  #include <QDialog>
                  #include <QLineEdit>
                  #include <QVariantAnimation>
                  #include <QPainter>
                  #include <QLabel>
                  #include <QWidget>
                  
                  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
                  
                  

                  dialog.cpp:

                  #include "dialog.h"
                  #include "ui_dialog.h"
                  #include "mylabel.h"
                  #include <QGraphicsDropShadowEffect>
                  #include <QLabel>
                  
                  Dialog::Dialog(QWidget *parent) :
                      QDialog(parent),
                      ui(new Ui::Dialog)
                  
                  {
                      ui->setupUi(this);
                  
                      mylabel = new MyLabel(this);
                  
                      label = new QLabel(this);
                      label->setGeometry(50,50,600,500);
                      label->show();
                      label->setObjectName("label");
                      label->setStyleSheet(
                      "  QLabel#label{"
                          "     background-color: pink;"
                                  "}"
                          );
                  
                      lineEdit_1 = new QLineEdit(label);
                      lineEdit_1->setGeometry(400,50,100,50);
                      lineEdit_1->show();
                  
                  
                  
                      connect(lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(setProgress()));
                  
                  
                  }
                  
                  
                  Dialog::~Dialog()
                  {
                      delete ui;
                  }
                  
                  void Dialog::setProgress()
                  {
                  
                      mylabel->SetProgress(120);
                      mylabel->update();
                  }
                  
                  

                  mylabel.h:

                  #ifndef MYLABEL_H
                  #define MYLABEL_H
                  #include <QObject>
                  #include <QLabel>
                  #include "dialog.h"
                  
                  class MyLabel : public QLabel
                  {
                      Q_OBJECT
                      public:
                          MyLabel(QWidget *parent = 0);
                          void paintEvent(QPaintEvent *);
                          void SetProgress(int progressValue );
                          int progress;
                  
                  };
                  
                  
                  #endif
                  
                  

                  mylabel.cpp:

                  #include "mylabel.h"
                  #include <QLabel>
                  
                  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);
                  }
                  
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @nanor said in How to paint on a label created in QDialog class?:

                  mylabel = new MyLabel(this);
                  
                  label = new QLabel(this);
                  

                  You have created a MyLabel, and then never used it?! Instead retaining all your code which creates a QLabel??

                  N 1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    As @JonB says
                    you want

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

                    so we use our custom one and not another plain one.

                    N 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @nanor said in How to paint on a label created in QDialog class?:

                      mylabel = new MyLabel(this);
                      
                      label = new QLabel(this);
                      

                      You have created a MyLabel, and then never used it?! Instead retaining all your code which creates a QLabel??

                      N Offline
                      N Offline
                      nanor
                      wrote on last edited by
                      #10

                      @JonB 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.

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        As @JonB says
                        you want

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

                        so we use our custom one and not another plain one.

                        N Offline
                        N Offline
                        nanor
                        wrote on last edited by
                        #11

                        @mrjj Hi. Yes you are right. Thank you so much. Really appreciate your help.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved