python-3.x 如何在PyQt5中画一条从鼠标到一个点的直线?

frebpwbc  于 2023-02-06  发布在  Python
关注(0)|答案(3)|浏览(255)

下面是我的代码:

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

我尝试做的是画一条简单的线从鼠标的位置到中间的小部件使用这个:

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

但是当我运行它的时候,没有一条线是可见的,我需要做什么?

nue99wik

nue99wik1#

必须实现函数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(), 500, 250)

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

输出:

b91juud3

b91juud32#

你只能在paintEvent方法中使用QPainter,所以一种修复的方法是记录类中的x和y坐标并更新根部件,然后调用paintEvent,你会看到这行。
范例

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

我不确定self.xself.y最初是如何设置的,paintEvent中的-1和check感觉有点笨拙,但至少它可以画。

e4yzc0pl

e4yzc0pl3#

对于前面的答案,我在Python3.7和PyQt 5下尝试了一下。结果是程序崩溃“进程结束,退出代码为-1073740791(0xC 0000409)”。在评论中,还有人也提到了崩溃。我找到了这个崩溃的解决方案:self.x和self.y必须在调用www.example.com()之前初始化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()

相关问题