我正在使用 qrect 和简单文本项制作图像注释系统。
我试图将 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());
}
关于c++ - 从 QGraphicsSimpleTextItem foreach 循环获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61563731/