Lar*_*lke 9 c++ qt transformation qgraphicsview
我正在使用Qt的QGraphicsView- 和 - QGraphicsItem子类.有没有办法在视图矩形更改时不缩放视图中项目的图形表示,例如放大时.默认行为是我的项目相对于我的视图矩形缩放.
我想要显示2d点,这些点应该由一个薄矩形表示,当在视图中放大时,该矩形不应该缩放.请参阅典型的三维建模软件以供参考,其中顶点始终以相同的大小显示.
谢谢!
Ari*_*yat 11
将QGraphicItem'标志设置QGraphicsItem::ItemIgnoresTransformations为true对您不起作用?
小智 6
我遇到了同样的问题,花了我一段时间才解决。这就是我解决的方法。
扩展QGraphicsItem类,重写paint()。在paint()内部,将转换的缩放比例重置为1(分别为m11和m22),并在重置之前保存m11(x缩放比例)和m22(y缩放比例)。然后,像平常一样绘制,但将x乘以m11,将y乘以m22。这样可以避免使用默认的变换进行绘制,而是根据场景的变换显式计算位置。
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
QTransform t = painter->transform();
qreal m11 = t.m11(), m22 = t.m22();
painter->save(); // save painter state
painter->setTransform(QTransform(m11, t.m12(), t.m13(),
t.m21(), 1, t.m23(), t.m31(),
t.m32(), t.m33()));
int x = 0, y = 0; // item's coordinates
painter->drawText(x*m11, y*m22, "Text"); // the text itself will not be scaled, but when the scene is transformed, this text will still anchor correctly
painter->restore(); // restore painter state
}
Run Code Online (Sandbox Code Playgroud)
以下代码块使用默认转换绘制
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
int x = 0, y = 0;
painter->drawText(x, y, "Text");
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试两者来查看差异。希望这可以帮助。