我有一个QGraphicsTextItem,其父项为QGraphicsItem。我希望QGraphicsTextItem始终位于QGraphicsItem的正上方,但我也希望当缩放因子小于1时,文本保持相同的大小,即当父图形项被缩小时,文本仍保持缩放因子为1时的大小。我发现在缩放因子小于1时将QGraphicsItem :: ItemIgnoresTransformations标志设置为true可以保留大小。
但是我似乎找不到一种方法来使文本的位置始终保持在QGraphicsItem上方。有没有办法做到这一点?我尝试使用deviceTransform()函数,但是随着滚动条的滚动,文本仍然会移出QGraphicsItem。更糟糕的是,一些文本项目开始“抖动”,即它们开始微微地连续改变位置,看起来像是在抖动。如果这是我需要使用的函数,那么我想我不知道如何正确使用它。
在我的QGraphicsItem构造函数中,我添加了一个QGraphicsTextItem:
fTextItem = new QGraphicsTextItem(getName(), this);
fTextItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
这是QGraphicsItem的绘制函数代码片段:
qreal lod = painter->worldTransform().m22();
if(lod <= 1.0) {
fTextItem-setFlag(QGraphicsItem::ItemIgnoresTransformations);
fTextItem->setPos(fTextItem->deviceTransform(view-viewportTransform()).inverted().map(view->mapFromScene(mapToScene(0,0))));
} else {
fTextItem->setFlag(QGraphicsItem::ItemIgnoresTransformations, false);
fTextItem->setPos(0, 0);
}