[Interest] QGraphicsObject::boundingRect() called with different instance

Bob Hood bhood2 at comcast.net
Wed Oct 30 01:47:08 CET 2019


I’m sure I’m being dense here, but I’m confused about why my QGraphicsItem 
subclass is being called with a different instance when the overrides of the 
boundingRect() and paint() functions are being called. Here’s the code…

Header:

|class Entity : public QGraphicsObject { Q_OBJECT public: explicit 
Entity(const QString& name, const QSizeF& size); QRectF boundingRect() const 
override; void paint(QPainter *painter, const QStyleOptionGraphicsItem 
*option, QWidget *widget) override; private: QSizeF entity_size; QString 
entity_name; }; |

Module:

|Entity::Entity(const QString& name, const QSizeF& size) : entity_name(name), 
entity_size(size) { setAcceptHoverEvents(true); setFlag(ItemIsFocusable, 
true); setFlag(ItemIsSelectable, true); setFlag(ItemIsMovable, true); } QRectF 
Entity::boundingRect() const { return QRectF(QPointF(0,0), entity_size); } 
void Entity::paint(QPainter *painter, const QStyleOptionGraphicsItem* 
/*option*/, QWidget* /*widget*/) { ... } |

Main:

|... scene = ScenePointer(new QGraphicsScene(-200, -200, 400, 400)); Entity* 
entity = new Entity("Box", QSizeF(100, 100)); entities.push_back(entity); 
entity->setPos(-150, -130); scene->addItem(entity); view = ViewPointer(new 
QGraphicsView(scene.data())); view->setRenderHint(QPainter::Antialiasing); 
view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); 
view->setBackgroundBrush(QColor(230, 200, 167)); 
setCentralWidget(view.data()); ... |

…and here’s the problem…

When I enter the constructor, I have a specific ‘this’ pointer that has the 
provided ‘size’ argument placed into its ‘entity_size’ member. However, when 
Qt subsequently invokes the boundingRect() override function, it’s an entirely 
different ‘this’ instance whose ‘entity_size’ has clearly not been initialized.

I’ve looked at several select examples/widgets/graphicsview/ sources, and 
those show that this should be working as expected.

I’ve tested this with 5.11.1 and 5.12.3 with the same result, so I’m guessing 
this is some kind of pilot error on my part.

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191029/d5209d35/attachment.html>


More information about the Interest mailing list