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. QPainter does not work
Forum Updated to NodeBB v4.3 + New Features

QPainter does not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpainterqlineqimageqwidgetqpaintevent
18 Posts 7 Posters 1.9k Views 2 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.
  • J Joe von Habsburg

    @Christian-Ehrlicher said in QPainter does not work:

    What? You wrote that QPainter is not working - you can only paint on a widget inside it's paintEvent().

    In my function, it did not work. Should I use paintEvent ?

    @SGaist said in QPainter does not work:

    no need to allocate the painter on the heap. This just make the code more complex for no benefits.

    Thank you

    J Offline
    J Offline
    Joe von Habsburg
    wrote on last edited by
    #6

    @Christian-Ehrlicher said in QPainter does not work:

    paintEvent().

    How can I send qpoints to paintEvent ? Could you give code example ?

    Christian EhrlicherC 1 Reply Last reply
    0
    • J Joe von Habsburg

      @Christian-Ehrlicher said in QPainter does not work:

      paintEvent().

      How can I send qpoints to paintEvent ? Could you give code example ?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @Joe-von-Habsburg said in QPainter does not work:

      How can I send qpoints to paintEvent ?

      You don't send anything. Just store them in your class.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Joe-von-Habsburg said in QPainter does not work:

        How can I send qpoints to paintEvent ?

        You don't send anything. Just store them in your class.

        J Offline
        J Offline
        Joe von Habsburg
        wrote on last edited by Joe von Habsburg
        #8

        @Christian-Ehrlicher said in QPainter does not work:

        You don't send anything. Just store them in your class.

        I wrote basic code. I send points and update widget. Lines have been drawn but I draw there QImage currently, and lines deleted

        
        class MyClass : public QObject{
           MyClass(){
              widget = new Widget(parentWidget)
              connect(this, &MyClass:sendPoints, widget, &Widget:takePointsFromClass)
           }
        
           public slots:
                 void takePoints(QPointF first, QPointF last){
                      emit sendPoints(first, last); 
                 }
        
           signals:
                 void sendPoints(QPointF first, QPointF last);
        
           private:
                Widget *widget;
        }
        
        
        class Widget : public QWidget {
           Widget();
           
            public slots:
               void takePointsFromClass(QPointF first, QPointF last){
                   _first.setX(first.x());
                   _first.setY(first.y());
                   _last.setX(last.x());
                   _last.setY(last.y());
                   _canDraw = true;
                    update();
               }
        
        
            protected:
                 void paintEvent(QPaintEvent *event) {
                     if(_canDraw ){
                         QPainter painter(this);
                         QLine line1(_first.x(), 0, _first.x(), height());  
                         QLine line2(_last.x(), 0, _last.x(), height());
                         painter.eraseRect(rect());
                         painter.drawLine(line1);
                         painter.drawLine(line2);
                         _canDraw = false;       
                     }      
                 }`
           
             private:
                    bool _canDraw = false;
                    QPointF _first;
                    QPointF _last; 
        
        
        C 1 Reply Last reply
        0
        • J Joe von Habsburg

          @Christian-Ehrlicher said in QPainter does not work:

          You don't send anything. Just store them in your class.

          I wrote basic code. I send points and update widget. Lines have been drawn but I draw there QImage currently, and lines deleted

          
          class MyClass : public QObject{
             MyClass(){
                widget = new Widget(parentWidget)
                connect(this, &MyClass:sendPoints, widget, &Widget:takePointsFromClass)
             }
          
             public slots:
                   void takePoints(QPointF first, QPointF last){
                        emit sendPoints(first, last); 
                   }
          
             signals:
                   void sendPoints(QPointF first, QPointF last);
          
             private:
                  Widget *widget;
          }
          
          
          class Widget : public QWidget {
             Widget();
             
              public slots:
                 void takePointsFromClass(QPointF first, QPointF last){
                     _first.setX(first.x());
                     _first.setY(first.y());
                     _last.setX(last.x());
                     _last.setY(last.y());
                     _canDraw = true;
                      update();
                 }
          
          
              protected:
                   void paintEvent(QPaintEvent *event) {
                       if(_canDraw ){
                           QPainter painter(this);
                           QLine line1(_first.x(), 0, _first.x(), height());  
                           QLine line2(_last.x(), 0, _last.x(), height());
                           painter.eraseRect(rect());
                           painter.drawLine(line1);
                           painter.drawLine(line2);
                           _canDraw = false;       
                       }      
                   }`
             
               private:
                      bool _canDraw = false;
                      QPointF _first;
                      QPointF _last; 
          
          
          C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #9

          Simple example:

          // widget.h
          #ifndef WIDGET_H
          #define WIDGET_H
          
          #include <QWidget>
          
          class Widget : public QWidget
          {
              Q_OBJECT
          
          public:
              Widget(QWidget *parent = nullptr);
              ~Widget();
          
          
              // QWidget interface
          protected:
              void mousePressEvent(QMouseEvent *event);
              void mouseReleaseEvent(QMouseEvent *event);
              void paintEvent(QPaintEvent *event);
          
          private:
              QImage mBackground;
              QPointF mFrom;
              QPointF mTo;
          };
          #endif // WIDGET_H
          
          
          // widget.cpp
          #include "widget.h"
          
          #include <QPaintEvent>
          #include <QPainter>
          
          Widget::Widget(QWidget *parent)
              : QWidget(parent)
          {
              mBackground.load("/tmp/paint/background.jpeg");
          }
          
          Widget::~Widget()
          {
          }
          
          void Widget::mousePressEvent(QMouseEvent *event)
          {
              mFrom = event->position();
              mTo = mFrom;
          
          }
          
          void Widget::mouseReleaseEvent(QMouseEvent *event)
          {
              mTo = event->position();
              update();
          }
          
          void Widget::paintEvent(QPaintEvent *event)
          {
              QPainter p(this);
              p.drawImage(event->rect(), mBackground);
              if (mFrom != mTo) {
                  QPen pen(Qt::red);
                  pen.setWidth(3);
                  p.setPen(pen);
                  p.drawLine(mFrom, mTo);
              }
              p.end();
          }
          

          2ba803df-face-4f41-8011-505c69954a11-image.png

          J 1 Reply Last reply
          2
          • C ChrisW67

            Simple example:

            // widget.h
            #ifndef WIDGET_H
            #define WIDGET_H
            
            #include <QWidget>
            
            class Widget : public QWidget
            {
                Q_OBJECT
            
            public:
                Widget(QWidget *parent = nullptr);
                ~Widget();
            
            
                // QWidget interface
            protected:
                void mousePressEvent(QMouseEvent *event);
                void mouseReleaseEvent(QMouseEvent *event);
                void paintEvent(QPaintEvent *event);
            
            private:
                QImage mBackground;
                QPointF mFrom;
                QPointF mTo;
            };
            #endif // WIDGET_H
            
            
            // widget.cpp
            #include "widget.h"
            
            #include <QPaintEvent>
            #include <QPainter>
            
            Widget::Widget(QWidget *parent)
                : QWidget(parent)
            {
                mBackground.load("/tmp/paint/background.jpeg");
            }
            
            Widget::~Widget()
            {
            }
            
            void Widget::mousePressEvent(QMouseEvent *event)
            {
                mFrom = event->position();
                mTo = mFrom;
            
            }
            
            void Widget::mouseReleaseEvent(QMouseEvent *event)
            {
                mTo = event->position();
                update();
            }
            
            void Widget::paintEvent(QPaintEvent *event)
            {
                QPainter p(this);
                p.drawImage(event->rect(), mBackground);
                if (mFrom != mTo) {
                    QPen pen(Qt::red);
                    pen.setWidth(3);
                    p.setPen(pen);
                    p.drawLine(mFrom, mTo);
                }
                p.end();
            }
            

            2ba803df-face-4f41-8011-505c69954a11-image.png

            J Offline
            J Offline
            Joe von Habsburg
            wrote on last edited by
            #10

            @ChrisW67 said in QPainter does not work:

            void Widget::paintEvent(QPaintEvent *event)
            {
            QPainter p(this);
            p.drawImage(event->rect(), mBackground);
            if (mFrom != mTo) {
            QPen pen(Qt::red);
            pen.setWidth(3);
            p.setPen(pen);
            p.drawLine(mFrom, mTo);
            }
            p.end();
            }

            I will try, Thank you :)

            @ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.

            jsulmJ JonBJ C 3 Replies Last reply
            0
            • J Joe von Habsburg

              @ChrisW67 said in QPainter does not work:

              void Widget::paintEvent(QPaintEvent *event)
              {
              QPainter p(this);
              p.drawImage(event->rect(), mBackground);
              if (mFrom != mTo) {
              QPen pen(Qt::red);
              pen.setWidth(3);
              p.setPen(pen);
              p.drawLine(mFrom, mTo);
              }
              p.end();
              }

              I will try, Thank you :)

              @ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Joe-von-Habsburg said in QPainter does not work:

              QImage is constantly refreshing and that is delete my lines

              What does this mean?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              1
              • J Joe von Habsburg

                @ChrisW67 said in QPainter does not work:

                void Widget::paintEvent(QPaintEvent *event)
                {
                QPainter p(this);
                p.drawImage(event->rect(), mBackground);
                if (mFrom != mTo) {
                QPen pen(Qt::red);
                pen.setWidth(3);
                p.setPen(pen);
                p.drawLine(mFrom, mTo);
                }
                p.end();
                }

                I will try, Thank you :)

                @ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #12

                @Joe-von-Habsburg
                If the image changes, for whatever reason, you have to redraw the lines when you redraw the background. Which your paintEvent() seems to do. So unless you are changing the image somewhere else I don't see how you get a new image but no lines.

                1 Reply Last reply
                0
                • J Joe von Habsburg

                  @ChrisW67 said in QPainter does not work:

                  void Widget::paintEvent(QPaintEvent *event)
                  {
                  QPainter p(this);
                  p.drawImage(event->rect(), mBackground);
                  if (mFrom != mTo) {
                  QPen pen(Qt::red);
                  pen.setWidth(3);
                  p.setPen(pen);
                  p.drawLine(mFrom, mTo);
                  }
                  p.end();
                  }

                  I will try, Thank you :)

                  @ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #13

                  @Joe-von-Habsburg said in QPainter does not work:

                  @ChrisW67 In my code, QImage is constantly refreshing and that is delete my lines.

                  Then you are saying that mBackground in my example is changing. When you change it, call update().

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Joe-von-Habsburg said in QPainter does not work:

                    QImage is constantly refreshing and that is delete my lines

                    What does this mean?

                    J Offline
                    J Offline
                    Joe von Habsburg
                    wrote on last edited by
                    #14

                    @jsulm said in QPainter does not work:

                    What does this mean?

                    I take data each 100ms maybe, and I draw QImage with that data. and my QIMage refreshing always

                    @JonB said in QPainter does not work:

                    you have to redraw the lines when you redraw the background.

                    I draw but it likes be a gif. I want to see a const line not a gif.

                    JonBJ C 2 Replies Last reply
                    0
                    • J Joe von Habsburg

                      @jsulm said in QPainter does not work:

                      What does this mean?

                      I take data each 100ms maybe, and I draw QImage with that data. and my QIMage refreshing always

                      @JonB said in QPainter does not work:

                      you have to redraw the lines when you redraw the background.

                      I draw but it likes be a gif. I want to see a const line not a gif.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #15

                      @Joe-von-Habsburg
                      I don't know what you are saying.

                      The only code of yours I see which draws an image is the drawImage() in your paintEvent(), and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.

                      If you have different code from what you have shown we cannot guess.

                      J 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Joe-von-Habsburg
                        I don't know what you are saying.

                        The only code of yours I see which draws an image is the drawImage() in your paintEvent(), and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.

                        If you have different code from what you have shown we cannot guess.

                        J Offline
                        J Offline
                        Joe von Habsburg
                        wrote on last edited by
                        #16

                        @JonB said in QPainter does not work:

                        The only code of yours I see which draws an image is the drawImage() in your paintEvent(), and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.
                        If you have different code from what you have shown we cannot guess.

                        
                        MyClass (){
                        widget= new Widget(containerImage->parentWidget());
                            widget->setFixedSize(containerImage->parentWidget()->size());
                        
                            _image = new QImage(_containerImage->width(), _containerImage->height(), QImage::Format_RGB32);
                        }
                        
                        
                        void draw(...)
                        {
                             //_containerImage is a QLabel
                            for(int x = 0; x < _image->width(); x++){
                                for(int y = _image->height() - 1; y > 0; y--){
                                    _image->setPixel(x, y , _image->pixel(x, y-1));
                                }
                            }
                            double step = static_cast<double>(sample) / (_containerImage->width());
                        
                            QList<double> list;
                        
                            for(int i = 0; i < _containerImage->width(); i++){
                                list.append(step * i);
                            }
                        
                            for(int i = 0; i < list.size(); i++){
                                int ii = round(list.at(i));
                                _image->setPixel(i, 0, getColor(ssd.samples[ii + startPoint].toDouble()).rgb());
                            }
                            _containerImage->setPixmap(QPixmap::fromImage(*_image));
                            emit setPoints(_first, _last);
                        }
                        
                        JonBJ 1 Reply Last reply
                        0
                        • J Joe von Habsburg

                          @JonB said in QPainter does not work:

                          The only code of yours I see which draws an image is the drawImage() in your paintEvent(), and that also draws the line on top of it. So I don't see where you are changing the image without drawing the line.
                          If you have different code from what you have shown we cannot guess.

                          
                          MyClass (){
                          widget= new Widget(containerImage->parentWidget());
                              widget->setFixedSize(containerImage->parentWidget()->size());
                          
                              _image = new QImage(_containerImage->width(), _containerImage->height(), QImage::Format_RGB32);
                          }
                          
                          
                          void draw(...)
                          {
                               //_containerImage is a QLabel
                              for(int x = 0; x < _image->width(); x++){
                                  for(int y = _image->height() - 1; y > 0; y--){
                                      _image->setPixel(x, y , _image->pixel(x, y-1));
                                  }
                              }
                              double step = static_cast<double>(sample) / (_containerImage->width());
                          
                              QList<double> list;
                          
                              for(int i = 0; i < _containerImage->width(); i++){
                                  list.append(step * i);
                              }
                          
                              for(int i = 0; i < list.size(); i++){
                                  int ii = round(list.at(i));
                                  _image->setPixel(i, 0, getColor(ssd.samples[ii + startPoint].toDouble()).rgb());
                              }
                              _containerImage->setPixmap(QPixmap::fromImage(*_image));
                              emit setPoints(_first, _last);
                          }
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #17

                          @Joe-von-Habsburg
                          I have no idea what _containerImage is or how this has any relationship at all to your Widget::paintEvent(), when that is called, or what mBackground is compared to _containerImage....

                          If you want help with code you have you have to actually show it. Revealing extra bits of code you have over time does not help.

                          1 Reply Last reply
                          1
                          • J Joe von Habsburg

                            @jsulm said in QPainter does not work:

                            What does this mean?

                            I take data each 100ms maybe, and I draw QImage with that data. and my QIMage refreshing always

                            @JonB said in QPainter does not work:

                            you have to redraw the lines when you redraw the background.

                            I draw but it likes be a gif. I want to see a const line not a gif.

                            C Offline
                            C Offline
                            ChrisW67
                            wrote on last edited by
                            #18

                            @Joe-von-Habsburg My example recast with a changing background:

                            // widget.h
                            #ifndef WIDGET_H
                            #define WIDGET_H
                            
                            #include <QWidget>
                            
                            class Widget : public QWidget
                            {
                                Q_OBJECT
                            
                            public:
                                Widget(QWidget *parent = nullptr);
                                ~Widget();
                            
                                // QWidget interface
                            protected:
                                void mousePressEvent(QMouseEvent *event);
                                void mouseReleaseEvent(QMouseEvent *event);
                                void paintEvent(QPaintEvent *event);
                            
                            private slots:
                                void setBackground();
                            
                            private:
                                QImage mBackground;
                                QPointF mFrom;
                                QPointF mTo;
                            };
                            #endif // WIDGET_H
                            
                            
                            // widget.cpp
                            #include "widget.h"
                            
                            #include <QPaintEvent>
                            #include <QPainter>
                            #include <QLinearGradient>
                            #include <QRandomGenerator>
                            #include <QTimer>
                            #include <QDebug>
                            
                            Widget::Widget(QWidget *parent)
                                : QWidget(parent)
                                , mBackground(500, 500, QImage::Format_RGB32)
                            {
                                setBackground();
                                QTimer *t = new QTimer(this);
                                connect(t, &QTimer::timeout, this, &Widget::setBackground);
                                t->start(1000);
                            }
                            
                            Widget::~Widget()
                            {
                            }
                            
                            void Widget::mousePressEvent(QMouseEvent *event)
                            {
                                mFrom = event->position();
                                mTo = mFrom;
                            
                            }
                            
                            void Widget::mouseReleaseEvent(QMouseEvent *event)
                            {
                                mTo = event->position();
                                update();
                            }
                            
                            void Widget::paintEvent(QPaintEvent *event)
                            {
                                QPainter p(this);
                                p.drawImage(event->rect(), mBackground);
                                if (mFrom != mTo) {
                                    QPen pen(Qt::red);
                                    pen.setWidth(3);
                                    p.setPen(pen);
                                    p.drawLine(mFrom, mTo);
                                }
                                p.end();
                            }
                            
                            void Widget::setBackground()
                            {
                                QPainter p(&mBackground);
                                const QRectF rectf(mBackground.rect());
                                QLinearGradient grad(rectf.topLeft(), rectf.bottomRight());
                                grad.setColorAt(0,  QColor::fromRgb(QRandomGenerator::global()->generate()));
                                grad.setColorAt(1,  QColor::fromRgb(QRandomGenerator::global()->generate()));
                                p.fillRect(mBackground.rect(), QBrush(grad));
                                p.end();
                                update();
                            }
                            
                            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