Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem
-
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:
Whenever I add an item, QGraphicsScene::drawBackground will be triggered to draw the item area.
-
hi
Well that is expected.
IF you look at
https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackgroundit 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#copyto 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. -
hi
Well that is expected.
IF you look at
https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackgroundit 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#copyto 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. -
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 -
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