Pyqt5 - 放大 QLabel
Pyqt5 - Zoom on a QLabel
我正在尝试应用我在该站点上找到的缩放示例,但它使用了 QGraphicsScene 和 QGraphicsView,而我应该使用简单的 QLabel。这是代码,但它不起作用。我可以放大 Qlabel 还是不可能?
缩放应与 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_())
此外,您能解释一下 zoom_out 函数的最后几行是做什么的吗?这是反转缩放的方法吗?
一种可能的解决方案是使用 QGraphicsEffect 来缩放 QLabel 的绘画。
注意:小部件的大小没有改变,但绘画被缩放了。
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_())
我正在尝试应用我在该站点上找到的缩放示例,但它使用了 QGraphicsScene 和 QGraphicsView,而我应该使用简单的 QLabel。这是代码,但它不起作用。我可以放大 Qlabel 还是不可能? 缩放应与 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_())
此外,您能解释一下 zoom_out 函数的最后几行是做什么的吗?这是反转缩放的方法吗?
一种可能的解决方案是使用 QGraphicsEffect 来缩放 QLabel 的绘画。
注意:小部件的大小没有改变,但绘画被缩放了。
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_())