带鼠标事件的 QPaint 平移比例尺

QPaint translation scale with mouseevent

我需要绘制一个带缩放的形状 Qwheel event。我试过 painter.scalepainter.translate,但它根本不影响形状的缩放。

想法是绘制不同的形状,例如下图中的这个,一个矩形,起始值为( 200, 300) 看起来很完美,甚至以 canvas widget 为中心。但是当具有更大的值 ( 500, 700 ) 时,如下图 第二个所示 放大并变得不需要.我想按比例绘制它,并能够用 (放大和缩小)mouse event 来绘制它。

可视化。

代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Foo(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)
        self.setGeometry(QtCore.QRect(200, 100, 700, 600))        
        self.paint = Paint()
        self.sizeHint()
        self.lay = QtWidgets.QVBoxLayout()
        self.lay.addWidget(self.paint)
        self.setLayout(self.lay)

class Paint(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Paint, self).__init__(parent)
        self.setBackgroundRole(QtGui.QPalette.Base)     
        self.setAutoFillBackground(True)
        self._width = 200
        self._height = 300


    def paintEvent(self, event):
        pen = QtGui.QPen()
        brush = QtGui.QBrush( QtCore.Qt.darkCyan, QtCore.Qt.Dense5Pattern)
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setPen(pen)
        painter.setBrush(brush)
        r = QtCore.QRect(QtCore.QPoint((self.width() - self._width)/ 2, (self.height() -self._height)/ 2), QtCore.QSize(self._width, self._height))
        painter.drawRect(r)
        painter.scale(0.4,0.4)
        painter.translate(50, 50)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Foo()
    w.show()
    sys.exit(app.exec_())

非常感谢任何帮助。谢谢

缩放是相对于左上角给出的,所以如果我们想让它相对于中心缩放,它必须首先移动到那里,缩放后它被放置在初始位置。对于比例因子的情况,使用wheelEvent的方法使用angleDelta(),乘以一个合适的因子。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Foo(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)
        self.setGeometry(QtCore.QRect(200, 100, 700, 600))        
        self.paint = Paint()
        self.sizeHint()
        self.lay = QtWidgets.QVBoxLayout()
        self.lay.addWidget(self.paint)
        self.setLayout(self.lay)

class Paint(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Paint, self).__init__(parent)
        self.setBackgroundRole(QtGui.QPalette.Base)     
        self.setAutoFillBackground(True)
        self.r = QtCore.QRect(QtCore.QPoint(), QtCore.QSize(200, 300))
        self._factor = 1.0

    def paintEvent(self, event):
        self.r.moveCenter(self.rect().center())
        pen = QtGui.QPen()
        brush = QtGui.QBrush( QtCore.Qt.darkCyan, QtCore.Qt.Dense5Pattern)
        painter = QtGui.QPainter(self)
        painter.setBrush(brush)
        painter.setPen(pen)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)

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

        painter.drawRect(self.r)

    def wheelEvent(self, event):
        self._factor *= 1.01**(event.angleDelta().y()/15.0)
        self.update()
        super(Paint, self).wheelEvent(event)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Foo()
    w.show()
    sys.exit(app.exec_())