在 QWidget 上的 QPixmap 上绘制点(pyqt5)
Drawing points on QPixmap on QWidget (pyqt5)
我有一个带有 QLayout 的 QWidget,上面有一个 QLabel。
我在标签上设置了一个 QPixmap。无论用户点击图像的什么地方,我都想画一个点。我定义了 mouseReleaseEvent(有效)和 paintEvent(但未绘制任何点)。我已经阅读了所有类似的问题,并且 none 的解决方案对我有用。有什么帮助吗?我的相关代码:
class ImageScroller(QtWidgets.QWidget):
def __init__(self, img):
QtWidgets.QWidget.__init__(self)
main_layout = QtWidgets.QVBoxLayout()
self._image_label = QtWidgets.QLabel()
self._set_image(img)
main_layout.addWidget(self._image_label)
main_layout.addStretch()
self.setLayout(main_layout)
def _set_image(self, img):
img = qimage2ndarray.array2qimage(img)
qimg = QtGui.QPixmap.fromImage(img)
self._img_pixmap = QtGui.QPixmap(qimg)
self._image_label.show()
def paintEvent(self, paint_event):
painter = QtGui.QPainter(self)
painter.begin(self)
painter.setPen(QtGui.QPen(QtCore.Qt.red))
pen = QtGui.QPen()
pen.setWidth(20)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPoint(300,300)
painter.drawLine(100, 100, 400, 400)
for pos in self.chosen_points:
painter.drawPoint(pos)
painter.end()
def mouseReleaseEvent(self, cursor_event):
self.chosen_points.append(QtGui.QCursor().pos())
self.update()
当你使用 QtGui.QCursor.pos()
是获取光标相对于屏幕的坐标,但是当你想绘制一个小部件时,你必须在小部件的坐标中,因为小部件有mapToGlobal()
方法:
self.mapFromGlobal(QtGui.QCursor.pos())
但在这种情况下还有另一种解决方案,您必须使用 returns mouseReleaseEvent
具有 pos()
方法中的信息的事件:
cursor_event.pos()
还有一个问题是你创建的label是在widget上面所以看不到点,最简单的就是直接用drawPixmap()
方法画QPixmap
完整代码:
from PyQt5 import QtWidgets, QtGui, QtCore
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
self.chosen_points = []
QtWidgets.QWidget.__init__(self)
self._image = QtGui.QPixmap("image.png")
def paintEvent(self, paint_event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(20)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPoint(300, 300)
painter.drawLine(100, 100, 400, 400)
for pos in self.chosen_points:
painter.drawPoint(pos)
def mouseReleaseEvent(self, cursor_event):
self.chosen_points.append(cursor_event.pos())
# self.chosen_points.append(self.mapFromGlobal(QtGui.QCursor.pos()))
self.update()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
我有一个带有 QLayout 的 QWidget,上面有一个 QLabel。 我在标签上设置了一个 QPixmap。无论用户点击图像的什么地方,我都想画一个点。我定义了 mouseReleaseEvent(有效)和 paintEvent(但未绘制任何点)。我已经阅读了所有类似的问题,并且 none 的解决方案对我有用。有什么帮助吗?我的相关代码:
class ImageScroller(QtWidgets.QWidget):
def __init__(self, img):
QtWidgets.QWidget.__init__(self)
main_layout = QtWidgets.QVBoxLayout()
self._image_label = QtWidgets.QLabel()
self._set_image(img)
main_layout.addWidget(self._image_label)
main_layout.addStretch()
self.setLayout(main_layout)
def _set_image(self, img):
img = qimage2ndarray.array2qimage(img)
qimg = QtGui.QPixmap.fromImage(img)
self._img_pixmap = QtGui.QPixmap(qimg)
self._image_label.show()
def paintEvent(self, paint_event):
painter = QtGui.QPainter(self)
painter.begin(self)
painter.setPen(QtGui.QPen(QtCore.Qt.red))
pen = QtGui.QPen()
pen.setWidth(20)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPoint(300,300)
painter.drawLine(100, 100, 400, 400)
for pos in self.chosen_points:
painter.drawPoint(pos)
painter.end()
def mouseReleaseEvent(self, cursor_event):
self.chosen_points.append(QtGui.QCursor().pos())
self.update()
当你使用 QtGui.QCursor.pos()
是获取光标相对于屏幕的坐标,但是当你想绘制一个小部件时,你必须在小部件的坐标中,因为小部件有mapToGlobal()
方法:
self.mapFromGlobal(QtGui.QCursor.pos())
但在这种情况下还有另一种解决方案,您必须使用 returns mouseReleaseEvent
具有 pos()
方法中的信息的事件:
cursor_event.pos()
还有一个问题是你创建的label是在widget上面所以看不到点,最简单的就是直接用drawPixmap()
方法画QPixmap
完整代码:
from PyQt5 import QtWidgets, QtGui, QtCore
class ImageScroller(QtWidgets.QWidget):
def __init__(self):
self.chosen_points = []
QtWidgets.QWidget.__init__(self)
self._image = QtGui.QPixmap("image.png")
def paintEvent(self, paint_event):
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._image)
pen = QtGui.QPen()
pen.setWidth(20)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPoint(300, 300)
painter.drawLine(100, 100, 400, 400)
for pos in self.chosen_points:
painter.drawPoint(pos)
def mouseReleaseEvent(self, cursor_event):
self.chosen_points.append(cursor_event.pos())
# self.chosen_points.append(self.mapFromGlobal(QtGui.QCursor.pos()))
self.update()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = ImageScroller()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())