且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 QWidget (pyqt5) 上的 QPixmap 上绘制点

更新时间:2023-02-20 22:00:38

当您使用 QtGui.QCursor.pos() 时正在获取光标相对于屏幕的坐标,但是当您使用 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()

另一个问题是你创建的标签在widget上方所以你看不到点,最简单的就是用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_())