简体   繁体   English

PyQt4:图形视图和Pixmap大小

[英]PyQt4: Graphics View and Pixmap Size

I'm developing a GUI using QTDesigner for some image processing tasks. 我正在使用QTDesigner开发用于某些图像处理任务的GUI。 I have two graphic views beneath each other in a grid layout. 我在网格布局中彼此之下有两个图形视图。 Both should display an image and later on I will add overlays. 两者都应显示图像,以后再添加叠加层。

I create my Pixmap img_pixmap and add it to my Scene. 我创建我的Pixmap img_pixmap并将其添加到我的场景中。 This scene is then added to my graphics view. 然后将该场景添加到我的图形视图中。 Since my image is much larger than my screen size I apply fitInView() . 由于我的图像远大于屏幕尺寸,因此我应用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. 如您所见,我要避免使用白色边框。

Okay, I did as suggested by Pavel: 好的,我按照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)

It works fine when you call this in each resizeEvent() . 当您在每个resizeEvent()调用此resizeEvent()时,它都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM