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. Signal emitted in paintEvent stops drawing

Signal emitted in paintEvent stops drawing

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.2k 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.
  • O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #1

    I added an emit to the beginning of paintEvent and drawing stops, program continues, but if I add it to the end it works fine. Why?

    Works
    
    void MyCanvas::paintEvent(QPaintEvent *e)
    {
        QPainter p(this);
        p.drawRect(drawableAreaRectangle);
    drawing code.
        QString st;
        st.append("AspectRatios Canvas = ");
        st.append(QString::number(drawableAreaAspectRatio));
        st.append("  Rectangle = ");
        st.append(QString::number(drawableAspectRatio));
        emit message(st);
    }
    Does not work
    void MyCanvas::paintEvent(QPaintEvent *e)
    {
        QString st;
        st.append("AspectRatios Canvas = ");
        st.append(QString::number(drawableAreaAspectRatio));
        st.append("  Rectangle = ");
        st.append(QString::number(drawableAspectRatio));
        emit message(st);
        QPainter p(this);
    drawing code
    
    }
    
        connect(ui->canvas, &MyCanvas::message,
             this,
            [this](const QString& st)
            {   this->ui->statusBar->clearMessage();
                this->ui->statusBar->showMessage(st, 0);});
    
    O 1 Reply Last reply
    0
    • O ofmrew

      I added an emit to the beginning of paintEvent and drawing stops, program continues, but if I add it to the end it works fine. Why?

      Works
      
      void MyCanvas::paintEvent(QPaintEvent *e)
      {
          QPainter p(this);
          p.drawRect(drawableAreaRectangle);
      drawing code.
          QString st;
          st.append("AspectRatios Canvas = ");
          st.append(QString::number(drawableAreaAspectRatio));
          st.append("  Rectangle = ");
          st.append(QString::number(drawableAspectRatio));
          emit message(st);
      }
      Does not work
      void MyCanvas::paintEvent(QPaintEvent *e)
      {
          QString st;
          st.append("AspectRatios Canvas = ");
          st.append(QString::number(drawableAreaAspectRatio));
          st.append("  Rectangle = ");
          st.append(QString::number(drawableAspectRatio));
          emit message(st);
          QPainter p(this);
      drawing code
      
      }
      
          connect(ui->canvas, &MyCanvas::message,
               this,
              [this](const QString& st)
              {   this->ui->statusBar->clearMessage();
                  this->ui->statusBar->showMessage(st, 0);});
      
      O Offline
      O Offline
      ofmrew
      wrote on last edited by
      #2

      @ofmrew More info. I also get:

      QBackingStore::endPaint() called with active painter on backingstore paint device
      QBackingStore::endPaint() called with active painter on backingstore paint device

      I added p.end(); and that stopped the above messages.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What does the method connected to message do ? Note that it's pretty unusual to emit in the paintEvent.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ O 2 Replies Last reply
        0
        • SGaistS SGaist

          Hi,

          What does the method connected to message do ? Note that it's pretty unusual to emit in the paintEvent.

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

          @SGaist
          It's at the bottom of his post, scroll down. A lambda, writing to the statusbar.

          I presume that does not re-enter his canvas' paint event?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            What does the method connected to message do ? Note that it's pretty unusual to emit in the paintEvent.

            O Offline
            O Offline
            ofmrew
            wrote on last edited by
            #5

            @SGaist I know it is unusual, but the values required were available it there..

            As for JonB's comment, I have no idea. But the code that works gives no errors.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB Thanks, the scroll didn't go far enough...

              As I don't know what MyCanvas is, it could be triggering repaints every time, so re-triggering the emit etc.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • SGaistS SGaist

                @JonB Thanks, the scroll didn't go far enough...

                As I don't know what MyCanvas is, it could be triggering repaints every time, so re-triggering the emit etc.

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

                @SGaist

                As I don't know what MyCanvas is

                https://doc.qt.io/archives/3.3/qcanvas.html ? ;-)

                O 1 Reply Last reply
                0
                • JonBJ JonB

                  @SGaist

                  As I don't know what MyCanvas is

                  https://doc.qt.io/archives/3.3/qcanvas.html ? ;-)

                  O Offline
                  O Offline
                  ofmrew
                  wrote on last edited by
                  #8

                  @JonB MyCanvas is a UI Widget that I have promoted. Simply that. The full code (that works) is posted below; note that is only one update(). It fails when the emit is moved before the first declaration of QPainter.

                  class MyCanvas : public QWidget
                  {
                      Q_OBJECT
                  public:
                      explicit MyCanvas(QWidget *parent = nullptr);
                      void paintEvent(QPaintEvent *e);
                      void resizeEvent(QResizeEvent *e);
                  
                      qreal       drawableAreaAspectRatio;
                      QPointF     drawableAreaCenter;
                      qreal       drawableAreaHeight;
                      qreal       drawableAreaWidth;
                      qreal       drawableAspectRatio{1.0};
                      QRectF      drawableAreaRectangle;
                  signals:
                      void message(QString st);
                  public slots:
                      void    newAspectRatio(qreal ar);
                  
                  };
                  MyCanvas::MyCanvas(QWidget *parent) : QWidget(parent)
                  {
                  
                  }
                  
                  void MyCanvas::paintEvent(QPaintEvent *e)
                  {
                      QPainter p(this);
                      QString st;
                      p.drawRect(drawableAreaRectangle);
                      p.setPen(Qt::red);
                      if (drawableAspectRatio < drawableAreaAspectRatio){
                          st.append("Height Constrained ");
                          qreal widthDelta = drawableAreaHeight * drawableAspectRatio;
                          qreal wd = widthDelta * 0.5;
                          qreal cx = drawableAreaCenter.rx();
                          p.drawRect(int(cx - wd), 20, int(wd + wd), int(drawableAreaHeight));
                      }else{
                          st.append("Width Constrained ");
                          qreal heightDelta = drawableAreaHeight / drawableAspectRatio;
                          qreal hd = (heightDelta * 0.5);
                          qreal w = drawableAreaWidth;
                          qreal cy = drawableAreaCenter.ry();
                          p.drawRect(20.0, int(cy - hd), int(w), int(hd + hd));
                      }
                      p.end();
                      st.append("AspectRatios Canvas = ");
                      st.append(QString::number(drawableAreaAspectRatio));
                      st.append("  Rectangle = ");
                      st.append(QString::number(drawableAspectRatio));
                      emit message(st);
                  }
                  
                  void MyCanvas::resizeEvent(QResizeEvent *e)
                  {
                      drawableAreaRectangle = QRectF(this->rect());
                      drawableAreaAspectRatio = drawableAreaRectangle.width()/drawableAreaRectangle.height();
                      drawableAreaRectangle.adjust(20.0, 20.0, -20.0, -20.0);
                      drawableAreaCenter = drawableAreaRectangle.center();
                      drawableAreaHeight = drawableAreaRectangle.height();
                      drawableAreaWidth = drawableAreaRectangle.width();
                  }
                  
                  void MyCanvas::newAspectRatio(qreal ar)
                  {
                      drawableAspectRatio = ar;
                      this->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