简体   繁体   English

如何在PyQt5中从鼠标画一条线到一个点?

[英]How to draw a line from mouse to a point in PyQt5?

Here is my code:这是我的代码:

import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt

class MouseTracker(QWidget):
    distance_from_center = 0
    def __init__(self):
        super().__init__()
        self.initUI()
        self.setMouseTracking(True)

    def initUI(self):
        self.setGeometry(200, 200, 1000, 500)
        self.setWindowTitle('Mouse Tracker')
        self.label = QLabel(self)
        self.label.resize(500, 40)
        self.show()

    def mouseMoveEvent(self, event):
        distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
        self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))       

        q = QPainter()  #Painting the line
        q.begin(self)
        q.drawLine(event.x(), event.y(), 250, 500)
        q.end()

    def drawPoints(self, qp, x, y):
        qp.setPen(Qt.red)
        qp.drawPoint(x, y)

app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())

What I'm trying to do is paint a simple line from the position of the mouse to the middle of the widget using this:我想要做的是使用这个从鼠标的位置到小部件的中间绘制一条简单的线:

        q = QPainter()  #Painting the line
        q.begin(self)
        q.drawLine(event.x(), event.y(), 250, 500)
        q.end()

But when I run it, no line is visible.但是当我运行它时,看不到任何行。 What do I need to do?我需要做什么?

You must implement the function QPaintEvent , in this function you must draw the line, in addition you must call the function update() to update the drawing.你必须实现函数QPaintEvent ,在这个函数中你必须画线,另外你必须调用函数update()来更新绘图。

import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt

class MouseTracker(QWidget):
    distance_from_center = 0
    def __init__(self):
        super().__init__()
        self.initUI()
        self.setMouseTracking(True)

    def initUI(self):
        self.setGeometry(200, 200, 1000, 500)
        self.setWindowTitle('Mouse Tracker')
        self.label = QLabel(self)
        self.label.resize(500, 40)
        self.show()
        self.pos = None

    def mouseMoveEvent(self, event):
        distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
        self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))       
        self.pos = event.pos()
        self.update()

    def paintEvent(self, event):
        if self.pos:
            q = QPainter(self)
            q.drawLine(self.pos.x(), self.pos.y(), 250, 500)


app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())

Output:输出:

在此处输入图片说明

You can only use a QPainter inside the paintEvent method.您只能在paintEvent方法中使用QPainter So one way to fix is to record the x and y coordinates inside the class and update the root widget.因此,修复的一种方法是在类中记录 x 和 y 坐标并更新根小部件。 This then calls paintEvent and you will see the line.然后调用paintEvent ,您将看到该行。

example例子

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt

class MouseTracker(QWidget):
    distance_from_center = 0
    def __init__(self):
        super().__init__()
        self.initUI()
        self.setMouseTracking(True)
        self.x = -1
        self.y = -1

    def initUI(self):
        self.setGeometry(200, 200, 1000, 500)
        self.setWindowTitle('Mouse Tracker')
        self.label = QLabel(self)
        self.label.resize(500, 40)
        self.show()

    def paintEvent(self, e):

        if not (self.x == -1 or self.y == -1):
            q = QPainter()  #Painting the line

            q.begin(self)

            q.drawLine(self.x, self.y, 250, 500)
            q.end()

    def mouseMoveEvent(self, event):
        distance_from_center = round(((event.y() - 250)**2 + (event.x() - 500)**2)**0.5)
        self.label.setText('Coordinates: ( %d : %d )' % (event.x(), event.y()) + "Distance from center: " + str(distance_from_center))

        self.x = event.x()
        self.y = event.y()

        self.update()

    def drawPoints(self, qp, x, y):
        qp.setPen(Qt.red)
        qp.drawPoint(x, y)

app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())

I wasn't sure about how self.x and self.y would be set initially.我不确定最初如何设置self.xself.y The -1 and the check in the paintEvent feels a bit hacky, but at least it paints. -1 和paintEvent的检查感觉有点hacky,但至少它可以绘制。

For the previous answer, I tried it under Python3.7 and PyQt5.对于之前的回答,我在 Python3.7 和 PyQt5 下尝试过。 The result was program crash 'Process finished with exit code -1073740791 (0xC0000409)'.结果是程序崩溃“进程已完成,退出代码为 -1073740791 (0xC0000409)”。 And in the comments, there were someone else also mentioned the crash.在评论中,还有其他人也提到了坠机事件。 I find the solution to this crash: self.x and self.y must be initialized before the calling of self.show() So I simply modified the code to as follows:我找到了这个崩溃的解决方案: self.x 和 self.y 必须在调用 self.show() 之前初始化所以我简单地将代码修改为如下:

def initUI(self):
    self.setGeometry(200, 200, 1000, 500)
    self.setWindowTitle('Mouse Tracker')
    self.label = QLabel(self)
    self.label.resize(500, 40)
    self.x = 100
    self.y = 100
    self.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM