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. repaint doesn't call paintEvent

repaint doesn't call paintEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.9k Views
  • 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.
  • D Offline
    D Offline
    DrZod
    wrote on last edited by DrZod
    #1

    I am trying to write a Paint like program and for a start i just want to draw simple rectangles.So I Inherited from QGraphicsView . The Idea is to just click on Canvas drag the mouse and draw a rect. But repaint() in mouseMoveEvent doesn't call/trigger paintEvent. I also checked viewports paintEvent and that doesn't get called either.

    Qt version : 5.7.1
    OS : Windows 7

    class Canvas : public QGraphicsView {
        Q_OBJECT
    public:
        Canvas(QWidget* parent = 0);
    protected:
        void mousePressEvent(QMouseEvent* e) override;
        void mouseMoveEvent(QMouseEvent* e) override;
        void paintEvent(QPaintEvent* e) override;
    private:
        QPointF firstPoint_;
        QPointF secondPoint_;
    };
    
    Canvas::Canvas(QWidget* parent ) : QGraphicsView(parent),m_drawingRectangle(false) ,m_currentColor(Qt::red){
        customViewport_ = new CustomViewport(this);
        setViewport(customViewport_);
    }
    
    void Canvas::mousePressEvent(QMouseEvent* e) {
        switch (e->button()) {
            case Qt::LeftButton: {
                customViewport_->setFirstPoint(e->pos());
                firstPoint_ = mapToScene(e->pos());
            }
        }
        QGraphicsView::mousePressEvent(e);
    }
    
    void Canvas::mouseMoveEvent(QMouseEvent* e) {
        secondPoint_ = mapToScene(e->pos());
        repaint();
        QGraphicsView::mouseMoveEvent(e);
    }
    
    void Canvas::paintEvent(QPaintEvent* e){
        QGraphicsView::paintEvent(e);
        QPainter painter(viewport());    //I have also tried with QPainter(this)
    
        painter.drawRect(QRectF(firstPoint_,secondPoint_));
    }
    
    jsulmJ KroMignonK 2 Replies Last reply
    0
    • D DrZod

      I am trying to write a Paint like program and for a start i just want to draw simple rectangles.So I Inherited from QGraphicsView . The Idea is to just click on Canvas drag the mouse and draw a rect. But repaint() in mouseMoveEvent doesn't call/trigger paintEvent. I also checked viewports paintEvent and that doesn't get called either.

      Qt version : 5.7.1
      OS : Windows 7

      class Canvas : public QGraphicsView {
          Q_OBJECT
      public:
          Canvas(QWidget* parent = 0);
      protected:
          void mousePressEvent(QMouseEvent* e) override;
          void mouseMoveEvent(QMouseEvent* e) override;
          void paintEvent(QPaintEvent* e) override;
      private:
          QPointF firstPoint_;
          QPointF secondPoint_;
      };
      
      Canvas::Canvas(QWidget* parent ) : QGraphicsView(parent),m_drawingRectangle(false) ,m_currentColor(Qt::red){
          customViewport_ = new CustomViewport(this);
          setViewport(customViewport_);
      }
      
      void Canvas::mousePressEvent(QMouseEvent* e) {
          switch (e->button()) {
              case Qt::LeftButton: {
                  customViewport_->setFirstPoint(e->pos());
                  firstPoint_ = mapToScene(e->pos());
              }
          }
          QGraphicsView::mousePressEvent(e);
      }
      
      void Canvas::mouseMoveEvent(QMouseEvent* e) {
          secondPoint_ = mapToScene(e->pos());
          repaint();
          QGraphicsView::mouseMoveEvent(e);
      }
      
      void Canvas::paintEvent(QPaintEvent* e){
          QGraphicsView::paintEvent(e);
          QPainter painter(viewport());    //I have also tried with QPainter(this)
      
          painter.drawRect(QRectF(firstPoint_,secondPoint_));
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @DrZod How did you check that paintEvent() is not called?
      Also, did you try update() instead of repaint()?

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

      D 1 Reply Last reply
      0
      • D DrZod

        I am trying to write a Paint like program and for a start i just want to draw simple rectangles.So I Inherited from QGraphicsView . The Idea is to just click on Canvas drag the mouse and draw a rect. But repaint() in mouseMoveEvent doesn't call/trigger paintEvent. I also checked viewports paintEvent and that doesn't get called either.

        Qt version : 5.7.1
        OS : Windows 7

        class Canvas : public QGraphicsView {
            Q_OBJECT
        public:
            Canvas(QWidget* parent = 0);
        protected:
            void mousePressEvent(QMouseEvent* e) override;
            void mouseMoveEvent(QMouseEvent* e) override;
            void paintEvent(QPaintEvent* e) override;
        private:
            QPointF firstPoint_;
            QPointF secondPoint_;
        };
        
        Canvas::Canvas(QWidget* parent ) : QGraphicsView(parent),m_drawingRectangle(false) ,m_currentColor(Qt::red){
            customViewport_ = new CustomViewport(this);
            setViewport(customViewport_);
        }
        
        void Canvas::mousePressEvent(QMouseEvent* e) {
            switch (e->button()) {
                case Qt::LeftButton: {
                    customViewport_->setFirstPoint(e->pos());
                    firstPoint_ = mapToScene(e->pos());
                }
            }
            QGraphicsView::mousePressEvent(e);
        }
        
        void Canvas::mouseMoveEvent(QMouseEvent* e) {
            secondPoint_ = mapToScene(e->pos());
            repaint();
            QGraphicsView::mouseMoveEvent(e);
        }
        
        void Canvas::paintEvent(QPaintEvent* e){
            QGraphicsView::paintEvent(e);
            QPainter painter(viewport());    //I have also tried with QPainter(this)
        
            painter.drawRect(QRectF(firstPoint_,secondPoint_));
        }
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @DrZod said in repaint doesn't call paintEvent:

        mouseMoveEvent doesn't call/trigger paintEvent.

        Maybe a silly question, but are you sure the item is visible?
        And, as written in documentation, it is better to use update() instead of repaint() to avoid possible recursive calls:

        Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        D 1 Reply Last reply
        0
        • J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          From what I understand from the Documentation, you're not supposed to do your manual painting on a QGraphicsView from the paint event, but rather use the drawForeground method.

          I also found this old topic:
          https://forum.qt.io/topic/93327/how-can-i-use-qpainter-to-paint-on-qgraphicsview
          should still be relevant for your case.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @DrZod How did you check that paintEvent() is not called?
            Also, did you try update() instead of repaint()?

            D Offline
            D Offline
            DrZod
            wrote on last edited by DrZod
            #5

            @jsulm I put a breakpoint in my paintEvent implementation.
            I have tried both of them,neither one triggers paintEvent

            1 Reply Last reply
            0
            • KroMignonK KroMignon

              @DrZod said in repaint doesn't call paintEvent:

              mouseMoveEvent doesn't call/trigger paintEvent.

              Maybe a silly question, but are you sure the item is visible?
              And, as written in documentation, it is better to use update() instead of repaint() to avoid possible recursive calls:

              Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.

              D Offline
              D Offline
              DrZod
              wrote on last edited by
              #6

              @KroMignon What item are you referring? and to answer your question yes I have tried update().

              KroMignonK 1 Reply Last reply
              0
              • D DrZod

                @KroMignon What item are you referring? and to answer your question yes I have tried update().

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @DrZod I am not aware about QGraphicsView, but I think your Canvas class should sub-class QGraphicsItem. I don't be sure it made sense to sub-class QGraphicsView... But I am not a specialist in this domaine.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                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