python - 在主窗口中添加子窗口
问题描述
我在主窗口中添加了一个可拖动的红色圆圈。红色圆圈可以移动到主窗口上您想要的任何位置。但我想设置一个允许红色圆圈移动的边界。可能应该用子窗口来完成?任何人都知道如何做到这一点?我走到这一步:
import sys
from PyQt5.QtWidgets import QApplication, QGraphicsView, QWidget, QGraphicsEllipseItem, QMainWindow, QGroupBox, QGraphicsScene, QHBoxLayout
from PyQt5.QtCore import Qt, QPointF, QRect
class MovingObject(QGraphicsEllipseItem):
def __init__(self, x, y, r):
#de meegegeven waardes gebruiken om beginpositie en grootte ellips te bepalen
super().__init__(0, 0, r, r)
self.setPos(x, y)
self.setBrush(Qt.red)
##mousePressEvent checkt of er wel of niet wordt geklikt
def mousePressEvent(self, event):
pass
##mouseMoveEvent is om de item te kunnen draggen
def mouseMoveEvent(self, event):
orig_cursor_position = event.lastScenePos()
updated_cursor_position = event.scenePos()
orig_position = self.scenePos()
updated_cursor_x = updated_cursor_position.x() - orig_cursor_position.x() + orig_position.x()
updated_cursor_y = updated_cursor_position.y() - orig_cursor_position.y() + orig_position.y()
self.setPos(QPointF(updated_cursor_x, updated_cursor_y))
class GraphicView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.setSceneRect(0, 0, 60, 60)
#waardes x, y, r waarvan x en y beginpositie van ellips is en r is straal van ellips
self.scene.addItem(MovingObject(0, 0, 40))
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(800, 500, 400, 400)
self.setWindowTitle("MainWindow")
#set GraphicView in Window
self.graphicView = GraphicView()
self.setCentralWidget(self.graphicView)
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
解决方案
首先,不需要重新实现鼠标事件以允许使用鼠标移动 QGraphicsItem,因为设置ItemIsMovable
标志就足够了。
一旦设置了标志,就需要过滤几何变化并对它们做出反应。
这是通过设置ItemSendsGeometryChanges
标志并重新实现itemChange()
函数来实现的。使用ItemPositionChange
更改,您可以在应用之前接收“未来”位置,并最终更改(并返回)接收到的值;返回值是将在移动期间应用的实际最终位置。
剩下的就是提供一个参考来检查它。
在以下示例中,我将场景矩形(而不是您所做的视图矩形)设置为更大的边距,并为项目设置这些边距;您显然可以QRectF
为此设置任何内容。
我还实现了该drawBackground()
功能以显示用作限制的场景矩形。
class MovingObject(QGraphicsEllipseItem):
def __init__(self, x, y, r):
super().__init__(0, 0, r, r)
self.setPos(x, y)
self.setBrush(Qt.red)
self.setFlag(self.ItemIsMovable, True)
self.setFlag(self.ItemSendsGeometryChanges, True)
self.margins = None
def setMargins(self, margins):
self.margins = margins
def itemChange(self, change, value):
if change == self.ItemPositionChange and self.margins:
newRect = self.boundingRect().translated(value)
if newRect.x() < self.margins.x():
# beyond left margin, reset
value.setX(self.margins.x())
elif newRect.right() > self.margins.right():
# beyond right margin
value.setX(self.margins.right() - newRect.width())
if newRect.y() < self.margins.y():
# beyond top margin
value.setY(self.margins.y())
elif newRect.bottom() > self.margins.bottom():
# beyond bottom margin
value.setY(self.margins.bottom() - newRect.height())
return super().itemChange(change, value)
class GraphicView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.scene.setSceneRect(0, 0, 120, 120)
self.movingObject = MovingObject(0, 0, 40)
self.scene.addItem(self.movingObject)
self.movingObject.setMargins(self.scene.sceneRect())
def drawBackground(self, painter, rect):
painter.drawRect(self.sceneRect())
请注意,图形视图框架非常强大,但很难真正了解和理解。鉴于您提出的问题(“可能应该使用子窗口来完成?”),很明显您仍然需要了解它是如何工作的,因为使用子窗口是完全不同的事情。
我强烈建议您仔细阅读它的文档以及与QGraphicsItem相关的所有内容(函数和属性),它是所有图形项的基类。
不应覆盖现有属性;scene()
是 QGraphicsView 的基本属性,因此您要么选择另一个名称作为实例属性 ( self.myScene = QGraphicsScene()
),要么只使用局部变量 ( scene = QGraphicsScene()
) 并始终self.scene()
在__init__
.
推荐阅读
- asp.net-mvc - 如何获取列表
从 Razor Page 使用打字稿? - powershell - Netadapter 切换器 PowerShell 脚本
- c# - 如何在 StepArgumentTransformation 中使用 REGEX 查找前缀字符串
- css - CSS 选择 1、2、3,然后是 7、8、9,然后是 13、14、15 等
- typescript - 如何在文档中保留设置?
- vba - 如何在 VBA 中添加一天并使用格式 fct
- android - 开始一项活动 10 次,但一次只能进行一次
- python - 集群数量未知的集群
- macos - 将 32 位动态库转换为 64 位的 osx
- node.js - 无法使用本地护照登录