qgraphicsscene画随机点
时间: 2023-12-31 19:14:00 浏览: 54
好的,以下是使用QGraphicsScene绘制随机点的示例代码:
```python
import random
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtGui import QPen, QColor
class MyScene(QGraphicsScene):
def __init__(self, parent=None):
super(MyScene, self).__init__(parent)
self.setSceneRect(-200, -200, 400, 400)
self.drawPoints()
def drawPoints(self):
pen = QPen(QColor(255, 0, 0), 3, Qt.SolidLine)
for i in range(50):
x = random.randint(-200, 200)
y = random.randint(-200, 200)
point = QPointF(x, y)
self.addEllipse(point.x(), point.y(), 5, 5, pen)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
view = QGraphicsView()
scene = MyScene()
view.setScene(scene)
view.show()
sys.exit(app.exec_())
```
这个示例代码会在QGraphicsScene中绘制50个随机点,每个点的坐标在(-200, -200)到(200, 200)的范围内随机生成。你可以根据自己的需要进行修改。
阅读全文