pyqt如何在graphicsview上画点
时间: 2023-09-24 16:14:24 浏览: 131
您好,关于您提出的问题,可以参考下面的代码示例:
```python
import sys
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
class MyGraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
# 添加坐标系
self.scene.addLine(0, 0, 0, 400, QPen(Qt.red))
self.scene.addLine(0, 0, 600, 0, QPen(Qt.red))
# 在 GraphicsView 上画点
self.drawPoint()
def drawPoint(self):
pen = QPen(Qt.blue)
pen.setWidth(5)
self.scene.addEllipse(100, 100, 5, 5, pen)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = MyGraphicsView()
view.show()
sys.exit(app.exec_())
```
在这个代码示例中,我们首先创建了一个 MyGraphicsView 类,继承自 QGraphicsView。然后在构造函数中,我们创建了一个 QGraphicsScene,并将其设置为 GraphicsView 的场景。
接着,我们在场景中添加了坐标系,并调用了 drawPoint() 方法,在场景上画了一个蓝色的点。
需要注意的是,在上述代码中,我们使用了 PyQt5.QtGui.QPainter 对象来画点。此外,我们还使用了 PyQt5.QtGui.QPen 对象,来设置画笔的颜色和宽度等属性。
希望以上内容能够帮助到您,如有疑问,欢迎在评论区提出。