开发者

QGraphicsView make scene scale to full width

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)


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


Apparently when using the wrong format in qDebug the output is unpredictable. When using:

qDebug("geo: %i %i", geometry().width(), geometry().height());

indeed the correct size is displayed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜