我有一个QGraphicsTextItem父对QGraphicsItem。我希望QGraphicsTextItem始终位于QGraphicsItem的正上方,但我还希望文本在缩放系数低于1时保持相同大小,即文本保持其缩放系数为1的大小,即使父图形项目缩小。我发现,当比例因子低于1时,将QGraphicsItem::ItemIgnoresTransformations
标志设置为true就能保持尺寸。应用QGraphicsItem :: ItemIgnoresTransformations后维护相对的子位置
但我似乎无法找到一种方式来让文本的位置总是保持在QGraphicsItem之上。有没有办法做到这一点?我尝试使用deviceTransform()
函数,但是当我滚动出来时,文本仍然从QGraphicsItem移开。更糟糕的是,一些文本项目开始“摇摆”,即他们开始不断地改变自己的位置,以至于看起来像在颤抖。如果这是我需要使用的功能,我想我不知道如何正确使用它。
在我添加了一个QGraphicsTextItem我的QGraphicsItem的构造函数:
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);
}
你不应该设置QGraphicsTextItem对象标志忽略父转型,而不是对的QGraphicsItem? – ArmenB 2017-01-12 18:01:59