简体   繁体   English

Pyqt5 - 放大 QLabel

[英]Pyqt5 - Zoom on a QLabel

I'm trying to apply a zoom example that I've found on this site but it uses a QGraphicsScene and a QGraphicsView while I should use a simple QLabel.我正在尝试应用我在此站点上找到的缩放示例,但它使用 QGraphicsScene 和 QGraphicsView,而我应该使用简单的 QLabel。 This is the code but it does not work.这是代码,但它不起作用。 Can I zoom on a Qlabel or is it impossible?我可以放大 Qlabel 还是不可能? The zoom should work with ctrl++ / ctrl+- shortcut.缩放应该与 ctrl++ / ctrl+- 快捷方式一起使用。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QLabel
class GuiZoom(QtWidgets.QMainWindow):
   factor = 1.5

   def __init__(self, parent=None):
       super(GuiZoom, self).__init__(parent)

   
       self.setFixedSize(800, 600)

    
       self.lblZoom = QLabel("Facciamo lo zoom")
       self.lblZoom.setStyleSheet("border:10px solid green")
    
       self.setCentralWidget(self.lblZoom)

       QtWidgets.QShortcut(
           QtGui.QKeySequence(QtGui.QKeySequence.ZoomIn),
           self.lblZoom,
           context=QtCore.Qt.WidgetShortcut,
           activated=self.zoom_in,
       )

       QtWidgets.QShortcut(
           QtGui.QKeySequence(QtGui.QKeySequence.ZoomOut),
           self.lblZoom,
           context=QtCore.Qt.WidgetShortcut,
           activated=self.zoom_out,
       )
   def zoom_in(self):
       scale_tr = QtGui.QTransform()
       scale_tr.scale(GuiZoom.factor, GuiZoom.factor)

       tr = self.lblZoom.transform() * scale_tr
       self.lblZoom.setTransform(tr)

   def zoom_out(self):
       scale_tr = QtGui.QTransform()
       scale_tr.scale(GuiZoom.factor, GuiZoom.factor)

       scale_inverted, invertible = scale_tr.inverted()

      if invertible:
           tr = self.lblZoom.transform() * scale_inverted
           self.lblZoom.setTransform(tr)


if __name__ == "__main__":
   import sys

   app = QtWidgets.QApplication(sys.argv)
   ui = GuiZoom()
   ui.show()
   sys.exit(app.exec_())

Moreover can you explain me what the last lines of zoom_out functions do?此外,你能解释一下 zoom_out 函数的最后几行是做什么的吗? Is this a way to invert the zoom?这是一种反转缩放的方法吗?

One possible solution is to use a QGraphicsEffect to scale the QLabel's painting.一种可能的解决方案是使用 QGraphicsEffect 来缩放 QLabel 的绘画。

Note: The size of the widget is not changed but the painting is scaled.注意:小部件的大小没有改变,但绘画被缩放了。

from PyQt5 import QtCore, QtGui, QtWidgets


class ZoomEffect(QtWidgets.QGraphicsEffect):
    factor = 1.5
    _scale = 1.0

    @property
    def scale(self):
        return self._scale

    @scale.setter
    def scale(self, scale):
        self._scale = scale
        self.update()

    def draw(self, painter):
        painter.setRenderHints(
            QtGui.QPainter.Antialiasing
            | QtGui.QPainter.SmoothPixmapTransform
            | QtGui.QPainter.HighQualityAntialiasing
        )
        center = self.sourceBoundingRect(QtCore.Qt.DeviceCoordinates).center()

        pixmap, offset = self.sourcePixmap(QtCore.Qt.DeviceCoordinates)
        painter.setWorldTransform(QtGui.QTransform())

        painter.translate(center)
        painter.scale(self.scale, self.scale)
        painter.translate(-center)

        painter.drawPixmap(offset, pixmap)

    def zoom_in(self):
        self.scale *= self.factor

    def zoom_out(self):
        self.scale /= self.factor


class GuiZoom(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(GuiZoom, self).__init__(parent)

        self.setFixedSize(800, 600)

        self.lblZoom = QtWidgets.QLabel("Facciamo lo zoom")
        self.lblZoom.setStyleSheet("border:10px solid green")

        self.zoom_effect = ZoomEffect()
        self.lblZoom.setGraphicsEffect(self.zoom_effect)

        self.setCentralWidget(self.lblZoom)

        QtWidgets.QShortcut(
            QtGui.QKeySequence(QtGui.QKeySequence.ZoomIn),
            self.lblZoom,
            context=QtCore.Qt.WindowShortcut,
            activated=self.zoom_effect.zoom_in,
        )

        QtWidgets.QShortcut(
            QtGui.QKeySequence(QtGui.QKeySequence.ZoomOut),
            self.lblZoom,
            context=QtCore.Qt.WindowShortcut,
            activated=self.zoom_effect.zoom_out,
        )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = GuiZoom()
    ui.show()
    sys.exit(app.exec_())

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

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