pyqt5如何在label中实现可用鼠标改变长和宽,可旋转的矩形,代码实现
时间: 2024-05-08 08:18:56 浏览: 126
鼠标移动旋转矩形代码
以下是实现可用鼠标改变长和宽,可旋转矩形的代码:
```python
from PyQt5.QtCore import Qt, QPoint, QRectF
from PyQt5.QtGui import QMouseEvent, QPainter, QPen
from PyQt5.QtWidgets import QWidget, QApplication, QLabel
class RotatableRect(QWidget):
def __init__(self, parent=None):
super(RotatableRect, self).__init__(parent)
self.setMouseTracking(True)
self.dragging = False
self.resizing = False
self.rotating = False
self.origin_x = 0
self.origin_y = 0
self.current_x = 0
self.current_y = 0
self.angle = 0
self.width = 100
self.height = 50
self.setFixedSize(self.width, self.height)
self.setCursor(Qt.CrossCursor)
def mousePressEvent(self, event: QMouseEvent) -> None:
self.origin_x = event.pos().x()
self.origin_y = event.pos().y()
if self.isInResizeRect(event.pos()):
self.resizing = True
elif self.isInRotateRect(event.pos()):
self.rotating = True
else:
self.dragging = True
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
self.dragging = False
self.resizing = False
self.rotating = False
self.setCursor(Qt.CrossCursor)
def mouseMoveEvent(self, event: QMouseEvent) -> None:
self.current_x = event.pos().x()
self.current_y = event.pos().y()
if self.dragging and event.buttons() == Qt.LeftButton:
self.move(self.pos() + event.pos() - QPoint(self.origin_x, self.origin_y))
elif self.resizing and event.buttons() == Qt.LeftButton:
self.width = max(self.width + self.current_x - self.origin_x, 20)
self.height = max(self.height + self.current_y - self.origin_y, 20)
self.setGeometry(self.x(), self.y(), self.width, self.height)
self.origin_x = self.current_x
self.origin_y = self.current_y
elif self.rotating and event.buttons() == Qt.LeftButton:
x = event.pos().x() - self.width / 2
y = event.pos().y() - self.height / 2
if x != 0:
self.angle = -y / x if x > 0 else (3 * 3.1415926535 - y) / x
else:
self.angle = 3.1415926535 if y < 0 else 0
self.update()
else:
if self.isInResizeRect(event.pos()):
self.setCursor(Qt.SizeFDiagCursor)
elif self.isInRotateRect(event.pos()):
self.setCursor(Qt.PointingHandCursor)
else:
self.setCursor(Qt.CrossCursor)
def isInResizeRect(self, pos: QPoint) -> bool:
return self.width - pos.x() < 10 and self.height - pos.y() < 10
def isInRotateRect(self, pos: QPoint) -> bool:
return abs(pos.x() - self.width / 2) < 10 and abs(pos.y() - self.height / 2) < 10
def paintEvent(self, event) -> None:
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 2, Qt.SolidLine))
painter.translate(self.width / 2, self.height / 2)
painter.rotate(self.angle * 180 / 3.1415926535)
painter.drawRect(QRectF(-self.width / 2, -self.height / 2, self.width, self.height))
painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
painter.drawLine(QPointF(self.width / 2, self.height / 2), QPointF(self.width / 2 + 20, self.height / 2))
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.label.setPixmap(self.getPixmap())
self.setGeometry(0, 0, 400, 300)
def getPixmap(self):
rect = RotatableRect()
pixmap = rect.grab()
return pixmap
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在 `MainWindow` 中设置一个 `QLabel`,并将其内容设置为上述代码中 `RotatableRect` 绘制的矩形的截图。这样就可以在 `QLabel` 中显示可拖动、可改变大小和旋转的矩形。
注意:本代码仅实现了矩形的基本拖动、改变大小和旋转功能,并没有考虑边界情况、触摸屏幕等情况。可以根据实际需求进行修改和完善。
阅读全文