从QGraphicsItem foreach循环中从QGraphicsSimpleTextItem获取字符串



我正在使用qrect和Simple文本项制作一个图像注释系统。

我正在尝试将QGraphicssimpletext项中的字符串值存储到JSON文件中,以保存和加载注释框。矩形可以很好地工作,但我不知道如何获得字符串值。这是我试图为每个项目循环的foreach,因为文本项目是矩形的子项,所以位置无关紧要。

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->?);
}

使用将简单文本和矩形项都添加到场景中

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

我只是想知道如何从简单的文本项中获得字符串值,以便保存和加载字符串和框,而不仅仅是当前系统可以做的框

您必须强制转换并验证指针是否为空:

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

最新更新