在PyQt5中的图像顶部绘制鼠标轨迹

时间:2018-07-23 09:14:33

标签: python python-3.x pyqt pyqt5

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

1 个答案:

答案 0 :(得分:2)

您要在var express = require('express'); var router = express.Router(); var controller = require('../controllers/coursescontroller'); router.get('/', function(req, res, next) { var records = controller.find_records(); console.log(records); // console shows undefined res.send(records); // doesn't send anything back when I make a get request }); 中使用drawLine。当您移动鼠标时,它将调用mouseMoveEvent,它将从最后一个位置开始绘制一条线,然后它将自行调用mouseMoveEvent

尝试一下:

paintEvent

enter image description here