允许调整大小以使pyqt中的pixmap较小

发布于 2025-02-13 10:50:38 字数 370 浏览 0 评论 0原文

我有一个Qlabel显示QPIXMAP,其水平和垂直策略设置为扩展。我已经编写了单独的代码,以根据小部件的大小自动缩放PixMap,但是无法调整窗口以使图像大小,并且导致它无法使其变小。如何自由调整窗口?

调整代码:

def resizeEvent(self, a0: QtGui.QResizeEvent):
    self.page.setPixmap(
        self.loader.img.scaled(
        self.page.width(), self.page.height(), QtCore.Qt.KeepAspectRatio
        )
    )

I have a QLabel showing a QPixmap that has both horizontal and vertical policies set to expanding. I have written separate code to automatically scale the pixmap according to the size of the widget, but the window cannot be resized to make the image smaller than it is, and it results in it not being able to be made smaller. How do I allow the window to be resized freely?

Resizing code:

def resizeEvent(self, a0: QtGui.QResizeEvent):
    self.page.setPixmap(
        self.loader.img.scaled(
        self.page.width(), self.page.height(), QtCore.Qt.KeepAspectRatio
        )
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2025-02-20 10:50:39

最简单的方法是从qwidget覆盖 paintEvent + code> qtransform 以缩放图像以适合。

from PyQt5 import QtWidgets, QtGui, QtCore

class ImageLabel(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        self._image = None

    def setImage(self, image):
        self._image = image
        self.update()

    def paintEvent(self, event):
        if self._image is None or self._image.isNull():
            return
        painter = QtGui.QPainter(self)
        width = self.width()
        height = self.height()
        imageWidth = self._image.width()
        imageHeight = self._image.height()
        r1 = width / imageWidth
        r2 = height / imageHeight
        r = min(r1, r2)
        x = (width - imageWidth * r) / 2
        y = (height - imageHeight * r) / 2
        painter.setTransform(QtGui.QTransform().translate(x, y).scale(r,r))
        painter.drawImage(QtCore.QPointF(0,0), self._image)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    label = ImageLabel()
    label.setImage(QtGui.QImage("path/to/image.jpg"))
    label.show()
    app.exec()

The easiest way is to inherit from QWidget override paintEvent and use QTransform to scale image to fit.

from PyQt5 import QtWidgets, QtGui, QtCore

class ImageLabel(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        self._image = None

    def setImage(self, image):
        self._image = image
        self.update()

    def paintEvent(self, event):
        if self._image is None or self._image.isNull():
            return
        painter = QtGui.QPainter(self)
        width = self.width()
        height = self.height()
        imageWidth = self._image.width()
        imageHeight = self._image.height()
        r1 = width / imageWidth
        r2 = height / imageHeight
        r = min(r1, r2)
        x = (width - imageWidth * r) / 2
        y = (height - imageHeight * r) / 2
        painter.setTransform(QtGui.QTransform().translate(x, y).scale(r,r))
        painter.drawImage(QtCore.QPointF(0,0), self._image)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    label = ImageLabel()
    label.setImage(QtGui.QImage("path/to/image.jpg"))
    label.show()
    app.exec()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文