问题:当QGraphicsItem
使用标记QGraphicsItem::ItemIgnoresTransformations
时,视图无法正确缩放并显示不必要的滚动条。
要重现,请在表单上放置QGraphicsView
并使用以下代码:
#include <QGraphicsScene>
#include <QGraphicsTextItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene();
// Set the scene to the view. Has to be done before
// transformation in order for the problem to occur.
ui->graphicsView->setScene(scene);
// Add some text, make it transformation-invariant.
QGraphicsTextItem* txt = scene->addText("Hello World!");
txt->setFlag(QGraphicsItem::ItemIgnoresTransformations);
// Scale the scene, re-calculate the bounding rect.
ui->graphicsView->scale(10, 5);
QRectF rect = scene->itemsBoundingRect();
scene->setSceneRect(rect);
}
这也适用于QGraphicsEllipseItem
或QGraphicsRectItem
等其他项目。
如果没有设置标志(只是注释掉行txt->setFlag...
),输出就是预期的:
然而,当设置了标志时,我希望滚动条消失,因为文本显然适合视图。但它看起来像这样:
我知道场景只会自动增长,但不会收缩到其内容,所以我在结尾处明确设置了场景矩形。但即使这样也无济于事。
在我看来,这是Qt中的一个错误,但也许我也只是误解了一些东西。知道问题(和解决方案)是什么?
使用Qt 5.5,Ubuntu 14.04。
PS:是的,场景永远不会被释放。这当然不是生产代码; - )
答案 0 :(得分:0)
我在QImage之上遇到了与 addLine 和 addEllipse 类似的问题。
这个问题似乎来自 QGraphicsItem :: ItemIgnoresTransformations 标志,它似乎扩大了场景以保持&#34;转换&#34;对象放置,但随后忽略了放置和大小的变换,但损坏已经完成,因为它错误地改变了场景比例。
我有一个可以帮助你的解决方法。 如果在定位忽略变换的对象后将场景比例重置为已知的正确值。它会避免你的问题。
例如: 的 m_firstImageScene-&GT; setSceneRect(m_firstImageRect); 强>