PyQt/Qt:如何在 Qlabel 小部件中拉伸图像? [英] PyQt/Qt: How to stretch an image in Qlabel widget?
问题描述
我想在我的应用中显示图像.我使用 QtDesigner 设计 UI,然后使用 pyqt 进行编码.问题是将显示的图像大于 UI 上的小部件大小.我参考官方演示:QT - 小部件图像查看器演示
I want to display an image in my app. I use QtDesigner to design UI, then use pyqt to coding. The problem is the image that will be shown is lager than the widget size on the UI. I refer to offical demo: QT - Widget Image Viewer Demo
添加imagelabel和scrollArea,代码如下:
add imagelabel and scrollArea, code as follows:
---- UI init ----
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
self.label.setObjectName(_fromUtf8("label"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.scrollArea.setWidget(self.label)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
---- function ----
filename = "./Penguins.jpg"
image = QtGui.QImage(filename)
pp = QtGui.QPixmap.fromImage(image)
lbl = QtGui.QLabel(self.label)
lbl.setPixmap(pp)
self.scrollArea.setWidgetResizable(True)
lbl.show()
但它不会拉伸图像,甚至没有出现滚动条!
but it doesn't stretch the image, even no scroll bar appear!
推荐答案
需要调用self.label.setScaledContents(true);
.这样 QLabel
将自身调整为像素图/图像的大小,并且滚动条将变得可见.请参阅此文档.
You need to call self.label.setScaledContents(true);
. So that QLabel
will resize itself to the size of pixmap/image and scroll-bar will get visible. See this documentation.
这篇关于PyQt/Qt:如何在 Qlabel 小部件中拉伸图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!