【发布时间】:2018-12-30 16:47:48
【问题描述】:
我是 PyQt5 的新手。我试图在跟踪我的鼠标的加载图像线的顶部绘制,即按下鼠标将导致绘制鼠标移动到的任何位置,而鼠标仍被按下,并在我们释放鼠标时停止。
我看到了This answer,它很有帮助,但我正在尝试做一些更复杂的事情。
我想我搞砸了这些事件。
我写的(不起作用):
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.drawing = False
self.lastPoint = QPoint()
self.image = QPixmap("picture.png")
self.setGeometry(100, 100, 500, 300)
self.resize(self.image.width(), self.image.height())
self.label = QLabel(self)
self.show()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.image)
pen = QPen(Qt.red, 3)
painter.setPen(pen)
painter.drawLine(self.lastPoint, event.pos())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = Menu()
sys.exit(app.exec_())
【问题讨论】:
标签: python python-3.x pyqt pyqt5