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. Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem
Qt 6.11 is out! See what's new in the release blog

Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.3k Views 1 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.
  • M Offline
    M Offline
    MaMingKun
    wrote on last edited by
    #1

    I want to set the QGraphicsScene‘s background image,But this will affect the item

    The code is similar:

    // Scene
    void CGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
    {
        painter->drawImage(rect, QImage(":/background/background.jpg"));
    }
    
    bool CGraphicsScene::event(QEvent *event)
    {
        switch (event->type()){
        case QEvent::GraphicsSceneMousePress : {
            QGraphicsSceneMouseEvent * pMouseEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
            CGraphicsItem *pItem = new CGraphicsItem(pMouseEvent->scenePos().x(), pMouseEvent->scenePos().y());
            this->addItem(pItem);
            pItem->setPos(pMouseEvent->scenePos());
            m_vtItems.push_back(pItem);
            break;
        }
        default:
            break;
        }
        return QGraphicsScene::event(event);
    }
    
    // item
    QRectF CGraphicsItem::boundingRect() const
    {
        qreal penWidth = 1;
        return QRectF((-m_siWidth - penWidth) / 2, (-m_siHeight - penWidth) / 2, m_siWidth + penWidth,
                      m_siHeight + penWidth);
    }
    
    void CGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setRenderHint(QPainter::Antialiasing, true); 
        QPainterPath oEllipsePath;
        QRectF oEllipseRect = { -m_siWidth/2, -m_siHeight/2, m_siWidth, m_siHeight };
        oEllipsePath.addEllipse(oEllipseRect);
        painter->fillPath(oEllipsePath, Qt::black);
    }
    

    Running diagram:
    ![alt text](image.png image url)

    Whenever I add an item, QGraphicsScene::drawBackground will be triggered to draw the item area.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      Well that is expected.
      IF you look at
      https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackground

      it says "The rect parameter is the exposed rectangle." that is the parameter rect tells the area that needs repainting. ( like a dirty rect)

      So when you add an item its bounding box needs to be repainted.

      So you can use the
      https://doc.qt.io/qt-5/qimage.html#copy

      to get the part of the image that needs to be updated/redrawn.

      Currently, you will draw the entire image into the rect. which works good first time but when
      rect becomes a dirty rect for new item. it's not what you want.

      M 1 Reply Last reply
      1
      • M Offline
        M Offline
        MaMingKun
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • mrjjM mrjj

          hi
          Well that is expected.
          IF you look at
          https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackground

          it says "The rect parameter is the exposed rectangle." that is the parameter rect tells the area that needs repainting. ( like a dirty rect)

          So when you add an item its bounding box needs to be repainted.

          So you can use the
          https://doc.qt.io/qt-5/qimage.html#copy

          to get the part of the image that needs to be updated/redrawn.

          Currently, you will draw the entire image into the rect. which works good first time but when
          rect becomes a dirty rect for new item. it's not what you want.

          M Offline
          M Offline
          MaMingKun
          wrote on last edited by
          #4

          @mrjj Thank you, Is it better to use QGraphicsItem as the background?

          mrjjM 1 Reply Last reply
          0
          • M MaMingKun

            @mrjj Thank you, Is it better to use QGraphicsItem as the background?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @MaMingKun

            Hi
            I think a background image is fine. that function for such things.
            You just need to fix the code so it only draws what is needed when the rest
            is much smaller than the area.
            Your current
            painter->drawImage(rect,... )
            will scale the image to the rect.
            So you need to keep as a member variable
            and copy the rect needed to draw in drawBackground

            M 1 Reply Last reply
            1
            • mrjjM mrjj

              @MaMingKun

              Hi
              I think a background image is fine. that function for such things.
              You just need to fix the code so it only draws what is needed when the rest
              is much smaller than the area.
              Your current
              painter->drawImage(rect,... )
              will scale the image to the rect.
              So you need to keep as a member variable
              and copy the rect needed to draw in drawBackground

              M Offline
              M Offline
              MaMingKun
              wrote on last edited by
              #6

              @mrjj OK, I will try what you say, Thanks

              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