PyQt4:图形视图和像素图大小 [英] PyQt4: Graphics View and Pixmap Size

查看:56
本文介绍了PyQt4:图形视图和像素图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QTDesigner 为某些图像处理任务开发 GUI.我在网格布局中有两个相互下方的图形视图.两者都应该显示一个图像,稍后我将添加叠加层.

I'm developing a GUI using QTDesigner for some image processing tasks. I have two graphic views beneath each other in a grid layout. Both should display an image and later on I will add overlays.

我创建了我的 Pixmap img_pixmap 并将其添加到我的场景中.然后将此场景添加到我的图形视图中.由于我的图像比我的屏幕尺寸大得多,所以我应用了 fitInView().在代码中:

I create my Pixmap img_pixmap and add it to my Scene. This scene is then added to my graphics view. Since my image is much larger than my screen size I apply fitInView(). In code:

self.img_pixmap_p =  self.img_scene.addPixmap(img_pixmap)
self.img_view.setScene(self.img_scene)
self.img_scene.setSceneRect(QtCore.QRectF())
self.img_view.fitInView(self.img_scene.sceneRect(), QtCore.Qt.KeepAspectRatio)

到目前为止,一切都很好,但我如何摆脱图像视图周围的空白?理想情况下,我希望我的像素图使用图形视图的全宽并保持纵横比,图形视图应相应地调整其高度.关于如何以直接的方式实现这一目标的任何想法?

So far, so good but how do i get rid of the white space around my image view? Ideally, I want my pixmap use the full width of the graphics view and to keep the aspect ratio, the graphics view should adjust its height accordingly. Any ideas on how to achieve that in a straight forward fashion?

这里有一张图片可以让我更好地了解我得到了什么:

Here an image to get a better idea of what I get:

如您所见,有白色边框,这是我想避免的.

As you can see, there are white borders, which I want to avoid.

推荐答案

好的,我按照 Pavel 的建议做了:

Okay, I did as suggested by Pavel:

img_aspect_ratio =  float(pixmap.size().width()) / pixmap.size().height() 
width = img_view.size().width()
img_view.setFixedHeight( width / img_aspect_ratio )
img_view.fitInView(img_scene.sceneRect(), QtCore.Qt.KeepAspectRatio)

当你在每个 resizeEvent() 中调用它时它工作正常.

It works fine when you call this in each resizeEvent().

这篇关于PyQt4:图形视图和像素图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆