如何在画布的任何位置创建一个可以在画布上任何位置移动的正方形 [英] How create a square at any position of canvas that can be moved at any position on the canvas

查看:54
本文介绍了如何在画布的任何位置创建一个可以在画布上任何位置移动的正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布中的随机位置创建了一个正方形,但我不知道如何通过将其拖动到所需位置将其移动到画布中的其他位置,请提出一些编辑或新方法来实现建议的任务,我边做边学.

I have created a square at random position in canvas, but I don't know how do I move it to somewhere else in canvas by dragging it to desired position, please suggest some edits or a new method to achieve the proposed task, I am learning while doing so.

附言附上输出窗口的截图.

P.S. Attached a screenshot of the output window.

import sys
from random import randint
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


class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        title="TeeSquare"
        left=500
        top=200
        width=500
        height=400
        iconName="square.jpg"
        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(iconName))
        self.setGeometry(left, top, width, height)
        self.should_paint_Rect = False
        self.windowcomponents()
        self.initUI()
        self.show()

    def initUI(self):
        if self.should_paint_Rect:
            self.label=QtWidgets.QLabel(self)
            self.label.setText("circle")

    def windowcomponents(self):
        button=QPushButton("Add", self)
        button.setGeometry(QRect(0, 0, 50, 28))
        button.setIcon(QtGui.QIcon("Add.png"))
        button.setToolTip("Create Square")
        button.clicked.connect(self.paintRect)


    def paintEvent(self, event):
        super().paintEvent(event)
        if self.should_paint_Rect:
            painter = QtGui.QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
            painter.drawRect(randint(0,500), randint(0,500), 100, 100)
            self.initUI()
            self.label.move(60,100)

    def paintRect(self, painter):
        self.should_paint_Rect = True
        self.update()

app = QApplication(sys.argv)
Rect=Window()
Rect.show()
sys.exit(app.exec_())

推荐答案

创建动态元素的逻辑是表示一组特定的特征,通过修改这些,元素被修改.

The logic of creating a dynamic element is to indicate a set of specific characteristics that by modifying these, the element is modified.

在这种情况下,您可以使用正方形的中心、正方形的尺寸等,并且必须通过可以从头开始创建的数据结构来实现数据,例如通过创建具有以下信息的类矩形,但在 Qt 中没有必要创建该元素,因为它已经存在并且是 QRect.

In this case you could use the center of the square, the dimensions of the square, etc. and that data must be implemented through a data structure that can be created from scratch for example by creating a class that has the information of the rectangle, but in Qt it is not necessary to create that element since it already exists and is QRect.

既然该元素已被识别,您可以创建一个 QRect,当按钮被按下时其左上角是随机的,并使用该 QRect 对其进行绘制.

Now that that element has been identified, you can create a QRect whose top-left is random when the button is pressed, and use that QRect to paint it.

对于拖动的程序是:

  • 获取鼠标点击位置.
  • 验证点击是否在矩形内.
  • 计算相对于矩形的位置.
  • 移动鼠标时,矩形的位置必须根据鼠标按下的位置进行更新.

综合以上几点,解决办法是:

Considering all of the above, the solution is:

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))
            painter.drawRect(self.rect)

    def mousePressEvent(self, event):
        if self.rect.contains(event.pos()):
            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_())

这篇关于如何在画布的任何位置创建一个可以在画布上任何位置移动的正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆