Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How do I remove previous paint and repaint in QGraphicsItem?

    General and Desktop
    3
    7
    880
    Loading More Posts
    • 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.
    • L
      lansing last edited by lansing

      I have things drawn on the my QGraphicsItem object, and I have a wheelEvent that's going to draw new stuff on the item. When the wheelEvent happened, I wanted the old drawing to be removed and painted with the new stuff. I tried using update(), but it only adds the new drawing on top of the old one, it doesn't remove the old one.

      void MyGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)
      {
              if (event->delta() > 0) {
                  setZoomFactor(zoomFactor + 1);            
              }
              else {
                  setZoomFactor(zoomFactor - 1);            
              }
      
              setNewStuff();
              update();
      }
      
      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        You could to clear your old background by your own -> e.g. with QPainter::fillRect()

        Qt has to stay free or it will die.

        L 1 Reply Last reply Reply Quote 0
        • L
          lansing @Christian Ehrlicher last edited by

          @Christian-Ehrlicher

          Can you elaborate more? I tried creating a clearPaint() inside the QGrpahicsItem class and called it before the update() in the wheelEvent but nothing changed.

          void MyGraphicsItem::clearPaint()
          {    
              QPainter *painter = new QPainter;
              QBrush blueBrush(Qt::blue);
              painter->fillRect(boundingRect(), blueBrush);
          }
          
          void MyGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)
          {
              # ...
              setNewStuff();
              clearPaint();
              update();
          }
          
          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @lansing last edited by

            @lansing said in How do I remove previous paint and repaint in QGraphicsItem?:

            QPainter

            QPainter should be used in paintEvent

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

            1 Reply Last reply Reply Quote 2
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              You can only paint inside QGrpahicsItem::paint()

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 3
              • L
                lansing last edited by

                Okay I got it. Using fillRect() in the paint() worked.

                void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                {    
                    QBrush darkYellowBrush(Qt::darkYellow);
                    QBrush redBrush(Qt::red);
                
                    painter->fillRect(boundingRect(), darkYellowBrush);
                    painter->drawline(...)
                }
                

                Every time an is update() called from other functions, fillRect() will paint the empty rectangle first and then do all the drawing after.

                Thanks for the help.

                1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  Then please mark the issue as solved, thx.

                  Qt has to stay free or it will die.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post