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.
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to