gpt4 book ai didi

python - QPaint 使用 mouseevent 进行平移缩放

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:52 25 4
gpt4 key购买 nike

我需要绘制一个带有缩放和Qwheel事件的形状。我尝试过painter.scale和painter.translate,但它根本不影响形状的缩放。

这个想法是绘制不同的形状,例如下图上的这个,一个起始值为( 200, 300) 的矩形 看起来很完美,甚至以 canvas 小部件 为中心。但当值较大时( 500, 700 ),如下图第二个所示,会放大并变得不需要。我想按比例绘制它,并能够使用鼠标事件来滚动(放大和缩小)它。

可视化。

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

我非常感谢任何帮助。谢谢

最佳答案

比例是相对于左上角给出的,因此如果我们希望它相对于中心进行缩放,则必须首先将其移动到那里,然后在缩放后将其放置在初始位置。对于比例因子的情况,请使用带有 angleDelta()wheelEvent 方法,并乘以适当的因子。

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

关于python - QPaint 使用 mouseevent 进行平移缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com