問題描述
我有一個帶有 QLayout 的 QWidget,上面有一個 QLabel.我在標簽上設置了一個 QPixmap.無論用戶單擊圖像的何處,我都想畫一個點.我定義了 mouseReleaseEvent(有效)和paintEvent(但沒有繪制點).我已經閱讀了所有類似的問題,但沒有一個解決方案對我有用.有什么幫助嗎?我的相關代碼:
I have a QWidget with a QLayout on which there is a QLabel. I set a QPixmap on the label. Wherever the user clicks on the image, I want to draw a point. I defined mouseReleaseEvent (which works) and paintEvent (but no points are drawn). I've read all similar questions and none of the solutions worked for me. Any help? My relevant code:
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()
方法:
When you use QtGui.QCursor.pos()
is getting the coordinates of the cursor with respect to the screen, but when you want to paint a widget you must be in the coordinates of the widget, for it the widget has the mapToGlobal()
method:
self.mapFromGlobal(QtGui.QCursor.pos())
但是在這種情況下還有另一種解決方案,您必須使用返回mouseReleaseEvent
的事件,該事件具有pos()
方法中的信息:
But in this case there is another solution, you must use the event that returns mouseReleaseEvent
that has the information in the pos()
method:
cursor_event.pos()
另一個問題是你創建的標簽在小部件上方所以你看不到點,最簡單的方法是直接用drawPixmap()
QPixmap> 方法.
Another problem is that the label you created is above the widget so you do not see the points, the easiest thing is to draw the QPixmap
directly with the drawPixmap()
method.
完整代碼:
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_())
這篇關于在 QWidget 上的 QPixmap 上繪制點(pyqt5)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!