我正在使用Qt的和子类。是否有一种方法可以在更改视图矩形时,例如缩放视图时,不对项目的图形表示进行缩放。默认行为是,我的项目会根据视图矩形进行缩放。
我想要可视化2D点,应该由一个细长的矩形来表示,在查看时不应进行缩放。请参考典型的3D建模软件,其中顶点点始终以相同的大小显示。
谢谢!
我想要可视化2D点,应该由一个细长的矩形来表示,在查看时不应进行缩放。请参考典型的3D建模软件,其中顶点点始终以相同的大小显示。
谢谢!
QGraphicItem
的标志QGraphicsItem :: ItemIgnoresTransformations
设置为true对您无效吗?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(1, 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");
}
你可以尝试两种方式来查看区别,希望这能帮助到你。
QTransform
实例化行中的m11
应该是1而不是其他值。 - Canol Gökel#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();
}
正如您所看到的,文本被放大了,但矩形没有。请注意,这不仅防止了矩形的缩放,还防止了其他变换。
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();
...
}
QPointF ref(500, 500);
QPointF vector = scaleValue * QPointF(100, 100);
painter->drawLine(ref+vector, ref-vector);
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);
}
这是我到目前为止发现的最好的方法,但我仍在不断尝试探索。