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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…