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 do I remove previous paint and repaint in QGraphicsItem?

How do I remove previous paint and repaint in QGraphicsItem?

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

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

      L 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

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

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @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();
        }
        
        jsulmJ 1 Reply Last reply
        0
        • L lansing

          @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();
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          2
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You can only paint inside QGrpahicsItem::paint()

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

            1 Reply Last reply
            3
            • L Offline
              L Offline
              lansing
              wrote on last edited by
              #6

              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
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then please mark the issue as solved, thx.

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

                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