【问题标题】:Drawing on top of image in PyQt5 tracing the mouse在 PyQt5 中跟踪鼠标的图像顶部绘图
【发布时间】: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


    【解决方案1】:

    您想在mouseMoveEvent 中使用drawLine。当你移动鼠标时,它会调用mouseMoveEvent,它会从最后一个位置画一条线,然后它会自己调用paintEvent

    试试这个:

    import sys
    from PyQt5.QtCore import Qt, QPoint
    from PyQt5.QtWidgets import QMainWindow, QApplication
    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.show()
    
        def paintEvent(self, event):
            painter = QPainter(self)
            painter.drawPixmap(self.rect(), self.image)
    
        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))
                painter.drawLine(self.lastPoint, event.pos())
                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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2022-11-02
      • 2017-08-03
      相关资源
      最近更新 更多