Harry Dale
Harry Dale

Reputation: 23

Getting a string from QGraphicsSimpleTextItem from QGraphicsItem foreach loop?

I am making an image annotation system Using qrect and Simple text items.

I am trying to store the string values from the QGraphicssimpletext items into a JSON file to save and load the annotation boxes. The rectangles work fine but I cannot understand how to get a string value. This is the foreach I am trying to loop through for each item, and as the text items are children of the rectangles the position does not matter.

foreach(QGraphicsItem* item, items())
{
    arrayPosX.push_back(item->x());
    arrayHeight.push_back(item->boundingRect().height());
    arrayWidth.push_back(item->boundingRect().width());
    arrayPosY.push_back(item->y());
    arrayAnnotation.push_back(item->?);
}

Both the simple text and rect items are added to the scene using

itemToDraw = new QGraphicsRectItem;
this->addItem(itemToDraw);
simpleTextToDraw = new QGraphicsSimpleTextItem;
this->addItem(simpleTextToDraw);

I would simply like to know how I could get the string values from the simple text item as to allow saving and loading of both strings and boxes, not just boxes which the current system can do.

Upvotes: 2

Views: 144

Answers (1)

eyllanesc
eyllanesc

Reputation: 244142

You have to cast and verify that the pointer is not null:

// ...
arrayPosY.push_back(item->y());
if(QGraphicsSimpleTextItem* text_item = qgraphicsitem_cast<QGraphicsSimpleTextItem *>(item)){
    arrayAnnotation.push_back(text_item->text());
}

Upvotes: 2

Related Questions