gpt4 book ai didi

c++ - QGraphicsItem 缩放产生太大的边界矩形

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:49 34 4
gpt4 key购买 nike

问题:当使用带有标志 QGraphicsItem::ItemIgnoresTransformationsQGraphicsItem 时,View 无法正确缩放并显示不必要的滚动条。

要重现,请在窗体上放置一个 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);
}

这也适用于其他项目,例如 QGraphicsEllipseItemQGraphicsRectItem

没有设置标志(简单地注释掉 txt->setFlag... 行),输出是预期的:

This is what the text looks like when being normally scaled.

但是,设置标志后,我希望滚动条消失,因为文本清楚地适合 View 。但它看起来像这样:

This is what the text looks like when ignoring transformation.

我知道 Scene 只会自动增长,但不会缩小到它的内容,所以我在最后明确设置了 Scene Rect。但即使这样也无济于事。

在我看来这是 Qt 中的一个错误,但也许我也只是误解了一些东西。知道问题(和解决方案)是什么吗?

使用 Qt 5.5、Ubuntu 14.04。

PS:是的,场景永远不会被释放。这当然不是生产代码 ;-)

最佳答案

我在 QImage 之上使用 addLineaddEllipse 时遇到了类似的问题。

问题似乎来自于 QGraphicsItem::ItemIgnoresTransformations 标志,它似乎放大了场景以保持“变形”对象的放置,但随后忽略了位置和大小的变换,但是损坏已经完成,因为它错误地更改了场景比例。

我有一个变通办法,或许能帮到您。如果在定位忽略变换的对象后将场景比例重置为已知的正确值。它将避免您的问题。

例如:m_firstImageScene->setSceneRect(m_firstImageRect);

关于c++ - QGraphicsItem 缩放产生太大的边界矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071246/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com