的QGraphicsView和的QGraphicsItem:鸵鸟政策规模项目缩放视图时RECT
-
10-07-2019 - |
题
我使用Qt's QGraphicsView
- 和QGraphicsItem
- 子类。
是有办法不缩放视图中的项的图形表示当视图矩形被改变时,例如放大时,缺省行为是我的项目相对于我的视图矩形比例的。
我想形象化应当由薄矩形视图中的缩放时,其不应比例来表示的2D点。见供参考,其中顶点在相同的大小总是所示的典型3D建模软件。
谢谢!
解决方案
设置QGraphicItem
的标志QGraphicsItem::ItemIgnoresTransformations
以真心对你不起作用?
其他提示
我得到了同样的问题,我花了一段时间来弄明白。这是我如何解决它。
延伸的QGraphicsItem类,重写涂料()。 里面的paint()方法,变换的缩放因子重置为1(这是M11和M22),并保存M11(X比例因子)复位前和M22(Y比例因子)。 然后,画就像你通常会做,但有M11和y与M22乘以你的X。这就避免了默认的改造图纸,但根据现场的转型明确计算出的位置。
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
}
下面的代码块与默认变换绘制
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
int x = 0, y = 0;
painter->drawText(x, y, "Text");
}
您可以尝试既看到的差异。希望这可以帮助。
这样如何:
#include <QtGui/QApplication>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>
#include <QtGui/QGraphicsRectItem>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene;
scene.addText("Hello, world!");
QRect rect(50, 50, 100, 100);
QGraphicsRectItem* recti = scene.addRect(rect);
QGraphicsView view(&scene);
// Set scale for the view
view.scale(10.0, 5.0);
// Set the inverse transformation for the item
recti->setTransform(view.transform().inverted());
view.show();
return app.exec();
}
可以看到文本被放大,但矩形是没有的。注意这并不仅防止对矩形但和其他变换的缩放。
我发现,如果我派生一个新类和reimpliment涂料函数I可以做
void MyDerivedQGraphicsItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
double scaleValue = scale();
double scaleX = painter->transform().m11();
setScale(scaleValue / scaleX);
QGraphicsSvgItem::paint(painter,option,widget);
}
这是我迄今发现做的最好的方式,但我仍然摆弄左右。
下面的解决方案完全为我工作:
void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
{
double scaleValue = scale()/painter->transform().m11();
painter->save();
painter->scale(scaleValue, scaleValue);
painter->drawText(...);
painter->restore();
...
}
我们也可以乘我们想保持它的大小不变的保存/恢复环境以外的其他mesures的scaleValue。
QPointF ref(500, 500);
QPointF vector = scaleValue * QPointF(100, 100);
painter->drawLine(ref+vector, ref-vector);
不隶属于 StackOverflow