QPaint与MouseEvent的翻译比例

时间:2018-10-29 15:22:09

标签: python python-3.x pyqt pyqt5 qpainter

我需要绘制一个具有缩放比例和Qwheel event的形状。我已经尝试过painter.scalepainter.translate,但是它根本不影响形状的缩放。

想法是绘制不同的形状,例如在 下图 上绘制的形状,其起始值为(200,300) strong>看起来很完美,甚至位于canvas widget的中心。但是当(500,700)值较大时(如下面的 秒数 所示),则会放大并变得多余。我想按比例绘制它,并能够用mouse event滚动 (放大和缩小)

可视化。

enter image description here

enter image description here

代码:

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_())

非常感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

缩放比例是相对于左上角给出的,因此,如果我们要相对于中心缩放比例,则必须首先将其移动到该位置,然后在缩放后将其放置在初始位置。对于比例因子,请使用wheelEventangleDelta()方法,再乘以适当的因子。

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_())

enter image description here

enter image description here

enter image description here