qt在QGraphicsView上指定位置生成一个按钮
时间: 2024-04-29 16:02:51 浏览: 141
如果您想在QGraphicsView上指定位置生成一个按钮,可以使用QGraphicsProxyWidget。下面是一个简单的例子:
```python
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QPushButton, QGraphicsProxyWidget
from PyQt5.QtCore import QPointF
# 创建QGraphicsView和QGraphicsScene
view = QGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
# 创建QPushButton并将其包装在QGraphicsProxyWidget中
button = QPushButton("Button")
proxy = QGraphicsProxyWidget()
proxy.setWidget(button)
# 将QGraphicsProxyWidget添加到场景中并设置位置
scene.addItem(proxy)
proxy.setPos(QPointF(100, 100))
# 显示视图
view.show()
```
在上面的代码中,我们创建了一个QGraphicsView和QGraphicsScene。然后,我们创建了一个QPushButton并将其包装在QGraphicsProxyWidget中。最后,我们将QGraphicsProxyWidget添加到场景中,并通过setPos方法设置其位置。
请注意,这只是一个简单的例子,您可能需要根据您的具体需求进行修改。
阅读全文
相关推荐













