我无法理解如何对QGraphicsItem
应用缩放和旋转。
我需要能够应用旋转和缩放(不一定保持纵横比),但我得到的结果完全出乎意料。
旋转必须围绕物品中心进行。我似乎没有问题做到这一点 - 但如果我尝试调试边界矩形,我会得到看似错误的值。
如果我不保持纵横比,而是得到了一个非常奇怪的扭曲,我已经苦苦挣扎了很长时间,以找到原因并加以纠正。我希望有人能找到一个解决方案。
对于许多项目 - 如矩形 - 我的解决方案是放弃调整大小 - 只需用给定大小的新项目替换该项目。 我甚至为像素图这样的东西这样做 (虽然可能会严重影响性能)。
但我不知道如何对文本或其他几种类型(svg...)执行此操作。
我正在尝试了解如何在旋转的项目上应用缩放,以及如何正确应用它。
以下代码是一个实验,我在其中缩放和旋转文本项,结果...请参见附图
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
void experimentScaling(QGraphicsScene* s)
{
QGraphicsTextItem* ref = new QGraphicsTextItem(); // a reference, not resized
ref->setPlainText("hello world");
s->addItem(ref);
ref->setDefaultTextColor(Qt::red);
ref->setRotation(45);
QGraphicsTextItem* t = new QGraphicsTextItem(); // text item to be experimented on
t->setPlainText("hello world");
s->addItem(t);
QTransform transform; // scale
transform.scale(10, 1);
t->setTransform(transform);
t->update();
QPointF _center = t->boundingRect().center();
qDebug("%f %f %f %f", t->boundingRect().left(), t->boundingRect().top(), t->boundingRect().right(), t->boundingRect().bottom()); // seems to be unscaled...
t->setTransformOriginPoint(_center); // rotation must be around item center - and seems to work even though the bounding rect gives wrong values above
t->setRotation(45); // skewed
t->update();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene s;
QGraphicsView view(&s);
s.setSceneRect(-20, -20, 800, 600);
view.show();
experimentScaling(&s);
return app.exec();
}
参考文本(红色)旋转45度,文本旋转45度并调整大小为10.1:
调整大小后的文本(黑色)应与参考文本(红色)具有相同的高度,但实际上比参考文本高得多;
边界矩形不再是一个矩形-它是倾斜的;
角度看起来比45度小得多;
还添加了一个调整大小但未旋转的参考文本:
请帮助我理解为什么会出现这种行为,以及我能做些什么来解决它。我已经尝试查看QGraphicsRotation,但是我无法弄清如何应用它...我只得到了移动而不是旋转。