QGraphicsView使场景缩放到全宽(QGraphicsView make scene scale to full width)

编程入门 行业动态 更新时间:2024-10-28 14:29:21
QGraphicsView使场景缩放到全宽(QGraphicsView make scene scale to full width)

我将QGraphicsView子类化,并在我的布局中显示小部件。 在视图中我自然有QGraphicsScene。

我可以完美地检索QGraphicsScene大小(即像我设置的2100x2970),但QGraphicsView小部件的几何不正确。 每当我调整主窗口的大小时,布局都会发生变化,视图窗口小部件也会发生变化。 当然,QGraphicsScene的大小无关紧要,无论它是什么。 但是通过视口显示滚动条和场景的小部件并没有为我提供正确的小部件宽度和高度。

在QtDesigner中,QGraphicsView小部件显示QWidget几何体为1126x643。 但我永远无法在子类QGraphicsView类中检索这些值。

当我知道宽度(1126或主窗口调整大小时的其他内容)时,我可以计算视图的比例因子(比例因子= 1126/2100),以使场景覆盖视图窗口小部件的整个宽度。

在QGraphicsView中:

qDebug("scene: %f %f", scene()->width(), scene()->height()); // works (2100x2970) qDebug("geo: %f %f", geometry().width(), geometry().height()); // does not (0x2970)

I subclassed QGraphicsView and show the widget in my layout. Within the view I naturally have QGraphicsScene.

I can retrieve the QGraphicsScene size perfectly fine (namely 2100x2970 like I set), however the geometry of the QGraphicsView widget is incorrect. Whenever I resize the main window, the layout changes and so does the view widget change. Of course, the size of QGraphicsScene is irrelevant and stays whatever it is. But the widget showing the scrollbars and the scene through the viewport just doesn't provide me with the correct widget width and height.

In the QtDesigner the QGraphicsView widget shows to have QWidget geometry of 1126x643. But I can never retrieve these values in the subclassed QGraphicsView class.

When I know the width (1126 or something else when main window is resized) I can calculate the scale factor for the view (scale factor = 1126 / 2100) to get the scene to cover the full width of the view widget.

In QGraphicsView:

qDebug("scene: %f %f", scene()->width(), scene()->height()); // works (2100x2970) qDebug("geo: %f %f", geometry().width(), geometry().height()); // does not (0x2970)

最满意答案

你看过QGraphicsView :: fitInView()吗? 此方法将自动为您执行所需的比例计算。

Have you looked at QGraphicsView::fitInView()? This method will do the needed scale calculations for you automatically.

更多推荐

本文发布于:2023-07-30 02:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   场景   到全宽   QGraphicsView   full

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!