Qt 如何在QGraphicsView中设置精确的场景矩形

Qt 如何在QGraphicsView中设置精确的场景矩形,qt,graphics,Qt,Graphics,我对QGraphicsView有问题,我不知道如何为QGraphicsView设置准确的视口。例如,我编写了以下代码: QGraphicsScene *scene = new QGraphicsScene(); QGraphicsEllipseItem * ellipse; QPen pen(Qt::red); QBrush brush(Qt::blue); ellipse = scene->addEllipse(10,10,100,100,pen, brush); ui->grap

我对QGraphicsView有问题,我不知道如何为QGraphicsView设置准确的视口。例如,我编写了以下代码:

QGraphicsScene *scene = new QGraphicsScene();
QGraphicsEllipseItem * ellipse;
QPen pen(Qt::red);
QBrush brush(Qt::blue);
ellipse = scene->addEllipse(10,10,100,100,pen, brush);
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(10,10,100,100);
我认为结果必须是视图内的一个圆,其直径等于视口的高度或宽度。
但当我运行程序时,它会在视图中显示一个中等大小的圆圈,这意味着graphicView的场景比我指定的要大。任何人都知道为什么吗?

我想你想要
fitInView()


我自己找到了解决办法: 假设exactRect正是要放大的矩形:

QRectF exactRect(20, 10, 300, 200);
ui->graphicsView->setSceneRect(exactRect);
ui->graphicsView->setCenterOn(exactRect.center());
QMatrix mtx;
mtx.scale(ui->graphicsView->width()/exactRect.width(),
          ui->graphicsView->height()/exactRect.height());
ui->graphicsView->setMatrix(mtx);

也许我不能很好地解释这个问题。我所要说的是,当我使用SetScen直立(..)时,GraphicsView的最终场景直立不是我所期望的。你怎么知道视图比你指定的要大?什么是
ui->graphicsView->scene()说什么?发布你期望的和得到的屏幕截图。上面说矩形正是我想要的,但我看到的是真实的视口比场景更大。正如我所说,布局的优先级高于场景矩形的大小。我想我完全误解了。我想当我指定一个矩形时,GraphicsView小部件精确地显示了该矩形,因此它将自动缩放和平移场景以覆盖精确的区域。我想我错了,我要相信的是,GraphicsView与缩放场景无关,它只是转换场景。那么,如何在GraphicsView中显示场景的确切矩形呢?
QRectF exactRect(20, 10, 300, 200);
ui->graphicsView->setSceneRect(exactRect);
ui->graphicsView->setCenterOn(exactRect.center());
QMatrix mtx;
mtx.scale(ui->graphicsView->width()/exactRect.width(),
          ui->graphicsView->height()/exactRect.height());
ui->graphicsView->setMatrix(mtx);