我正在使用 Qt 的QGraphicsView
- 和 -QGraphicsItem
子类。有没有办法在视图矩形更改时不缩放视图中项目的图形表示,例如放大时。默认行为是我的项目相对于我的视图矩形进行缩放。
我想可视化 2d 点,这些点应该由一个细长的矩形表示,在放大视图时不应缩放。请参阅典型的 3D 建模软件以供参考,其中顶点始终以相同大小显示。
谢谢!
我正在使用 Qt 的QGraphicsView
- 和 -QGraphicsItem
子类。有没有办法在视图矩形更改时不缩放视图中项目的图形表示,例如放大时。默认行为是我的项目相对于我的视图矩形进行缩放。
我想可视化 2d 点,这些点应该由一个细长的矩形表示,在放大视图时不应缩放。请参阅典型的 3D 建模软件以供参考,其中顶点始终以相同大小显示。
谢谢!
将QGraphicItem
' 标志设置QGraphicsItem::ItemIgnoresTransformations
为 true 对您不起作用?
我遇到了同样的问题,我花了一段时间才弄清楚。这就是我解决它的方法。
扩展一个 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(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");
}
您可以尝试两者来查看差异。希望这可以帮助。
这个怎么样:
#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();
...
}
我们还可以将 scaleValue 乘以我们希望在保存/恢复环境之外保持其大小不变的其他度量。
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);
}
这是迄今为止我发现的最好的方法,但我仍在修补。