I have a QGraphicsItem
element (subclassesed from QGraphicsItem
) that has as child a QGraphicsTextItem
.
The problem is that the paint(...)
method of the QGraphicsItem
is called infinitely.
Here is the paint method from my QGraphicsItem
element with the QGraphicsTextItem
:
void rectangle_element::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
painter->drawRoundedRect( -10, -10, 80, 40, 5, 5 );
painter->drawStaticText( -10, -10, QStaticText( "some text" ) );
text_item->setPlainText( "more text" );
}
I cannot use setCacheMode
with other flag than QGraphicsItem::CacheMode::NoCache
.
I'm using Qt 5.6.
UPDATE:
text_item
is a member of the class and it is initialized in the initialization list.You set the text of text_item
in the paint()
method of the parent item, which seems to in turn trigger a repaint of the parent, thus creating an infinite loop. Generally, it's not a good idea to change items during paint operations. Keep in mind that the paint()
method can be called with a high frequency.
Probably there is some event triggering the need for a text change of text_item
, like user interaction, which in turn calls a slot. This is the point where you should set the text. The drawing of the text itself is handled by the QGraphicsTextItem
.