【发布时间】:2017-07-10 18:06:33
【问题描述】:
我是 Pyqt 的新手,我正在尝试创建一个 UI,当我们给出像素位置时,它会在图像 (QLabel) 上打印一个圆圈(使用 QPainter)。为了测试,我正在使用鼠标跟踪事件,但我想我在调整图像或标签大小时遇到了问题。下面是我实现的图片:
结果:
看看指针和圆的位置。这就是我要解决的问题。
您可以在下面看到代码(我正在使用 PIL(加载图像)和 PyQt5):
...
self.image = Image.open("Images/Quart_top_View_draw.jpg")
self.MainWindow.TennisCourtImage.setMouseTracking(True)
self.MainWindow.TennisCourtImage.mouseMoveEvent = self.get_mouse_pos
def get_mouse_pos(self,event):
x = event.pos().x()
y = event.pos().y()
print("Position: ", x,y)
img = self.image
width, height = img.size
print("Image Size: ", width, height)
img = ImageQt(img)
pixmap_image = QtGui.QPixmap.fromImage(img)
self.painterInstance = QtGui.QPainter(pixmap_image)
self.painterInstance.setPen(QPen(Qt.red, 15, Qt.SolidLine))
self.painterInstance.drawEllipse(x,y,15,15)
self.painterInstance.end()
myScaledPixmap = pixmap_image.scaled(self.MainWindow.TennisCourtImage.size(), Qt.KeepAspectRatio)
self.MainWindow.TennisCourtImage.setPixmap(myScaledPixmap)
...
有人可以帮帮我吗?
问候,
加布里埃尔:D
【问题讨论】:
-
你为什么使用 PIL?
-
你的问题是什么?,你想得到什么?
-
PIL 不是必需的。我可以改变我在哪里使用 PIL 作为 pixmap_image = QtGui.QPixmap("Images/Quart_top_View_draw.jpg") 行。我需要的是画出我的指针所在的圆圈。
-
我猜体育场的形象是QLabel,对吗?
-
是的,你是对的。当我移动鼠标时,我得到了他在 QLabel 上的位置。然后我在图像上画了一个圆圈,使用我从鼠标跟踪中获得的像素,但它不匹配(指针位置与图像上的圆圈不同)。当我最大化 UI 时,差异变得更大。我想这是规模问题,但我不知道如何解决。
标签: python qt pyqt pyqt4 pyqt5