0
我有一個QLidget,上面有一個QLabel,上面有一個QLabel。 我在標籤上設置了一個QPixmap。無論用戶點擊圖像,我想畫一個點。我定義了mouseReleaseEvent(哪個工作)和paintEvent(但沒有繪製點)。我已閱讀所有類似的問題,但沒有任何解決方案適用於我。任何幫助?我的相關代碼:在QWidget上繪製QPixmap上的點(pyqt5)
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()
哪裏是你'mouseReleaseEvent'實現?你有沒有調用'update()'方法? – eyllanesc
是的,我有。我使用mouseReleaseEvent實現編輯了帖子 –