如何通过在 PyQt5 中简单地拖动来移动图形(使用 paintEvent 创建)

How to move a figure(created using paintEvent) by simply draging it in PyQt5

我在 canvas 的随机位置创建了一个圆圈,但我无法通过单击并拖动它来移动或编辑其属性(如标签)。 我想创建一个可以通过拖动移动的圆圈,它的标签等属性可以随时编辑,请建议编辑或新方法。我是初学者 请帮助...

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QPushButton,QWidget
from PyQt5 import QtGui
from PyQt5.QtCore import QRect,Qt
from PyQt5.QtGui import QPainter,QBrush, QPen
from PyQt5 import QtCore
from random import randint

class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        title="layout management"
        left=500
        top=200
        width=500
        height=400
        iconName="Ash.jpg"
        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(iconName))
        self.setGeometry(left, top, width, height)
        self.should_paint_circle = False
        self.windowcomponents()
        self.initUI()
        self.show()
    def initUI(self):
        if self.should_paint_circle:
            self.label=QtWidgets.QLabel(self)
            self.label.setText('<h2>circle<h2>')
    def windowcomponents(self):
        button=QPushButton("Add", self)
        button.setGeometry(QRect(0, 0, 50, 28))
        button.setIcon(QtGui.QIcon("addbutton.png"))
        button.setToolTip("<h3>This is for creating random circles<h3>")
        button.clicked.connect(self.paintcircle)
        button=QPushButton("Generate Report", self)
        button.setGeometry(QRect(49,0,150,28))
        button.setIcon(QtGui.QIcon("generatereport.png"))
        button.setToolTip("This is for generating pdf report of connection between two circles")
        button=QPushButton("Save", self)
        button.setGeometry(QRect(199,0,120,28))
        button.setIcon(QtGui.QIcon("saveicon.png"))
        button.setToolTip("This is for saving an image of canvas area")

    def paintEvent(self, event):
        super().paintEvent(event)
        if self.should_paint_circle:
            painter = QtGui.QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
            painter.drawEllipse(randint(0,500), randint(0,500), 100, 100)
            self.initUI()
            self.label.move(60,100)
    def paintcircle(self, painter):
        self.should_paint_circle = True
        self.update()
app = QApplication(sys.argv)
circle=Window()
circle.show()
sys.exit(app.exec_())

图像在 window 中的随机位置显示一个圆圈,它不可拖动

解决方法类似,代码仅在如何判断按下的点是否在圆内变化:

import random
import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()

        self.rect = QtCore.QRect()
        self.drag_position = QtCore.QPoint()

        button = QtWidgets.QPushButton("Add", self)
        button.clicked.connect(self.on_clicked)

        self.resize(640, 480)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        if self.rect.isNull():
            self.rect = QtCore.QRect(
                QtCore.QPoint(*random.sample(range(200), 2)), QtCore.QSize(100, 100)
            )
            self.update()

    def paintEvent(self, event):
        super().paintEvent(event)
        if not self.rect.isNull():
            painter = QtGui.QPainter(self)
            painter.setRenderHint(QtGui.QPainter.Antialiasing)
            painter.setPen(QtGui.QPen(QtCore.Qt.black, 5, QtCore.Qt.SolidLine))
            <b>painter.drawEllipse(self.rect)</b>

    def mousePressEvent(self, event):
        if (
            <b>2 * QtGui.QVector2D(event.pos() - self.rect.center()).length()
            < self.rect.width()</b>
        ):
            self.drag_position = event.pos() - self.rect.topLeft()
        super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if not self.drag_position.isNull():
            self.rect.moveTopLeft(event.pos() - self.drag_position)
            self.update()
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        self.drag_position = QtCore.QPoint()
        super().mouseReleaseEvent(event)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Rect = Window()
    Rect.show()
    sys.exit(app.exec_())