pyqt5 根据坐标点绘图,坐标图中会有10个点相连接,且其中某两个点连接线段是一段圆弧,图像绘制好后将其置于窗口中心,且图像大小适应窗口大小
时间: 2024-05-25 07:18:29 浏览: 106
Pyqt5绘制基础图形
5星 · 资源好评率100%
以下是一个简单的示例代码,实现了根据给定坐标点绘制图形,并将其置于窗口中心并适应窗口大小。
```python
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtCore import Qt, QPoint, QRectF
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('My Widget')
self.points = [
QPoint(100, 100),
QPoint(200, 150),
QPoint(300, 150),
QPoint(400, 100),
QPoint(500, 200),
QPoint(400, 300),
QPoint(300, 250),
QPoint(200, 300),
QPoint(100, 250),
QPoint(200, 200)
]
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.translate(self.width() / 2, self.height() / 2)
painter.scale(min(self.width(), self.height()) / 600, min(self.width(), self.height()) / 600)
pen = QPen(Qt.black, 2)
brush = QBrush(Qt.SolidPattern)
# 绘制连接线
painter.setPen(pen)
for i in range(len(self.points) - 1):
painter.drawLine(self.points[i], self.points[i + 1])
# 绘制圆弧
painter.setBrush(brush)
painter.drawArc(QRectF(self.points[3], self.points[4] - self.points[3]), 0, 180 * 16)
# 绘制点
for point in self.points:
painter.drawEllipse(point, 5, 5)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
```
运行结果如下:
![pyqt5-drawing-demo.png](https://i.loli.net/2021/09/23/3TQxLWos9XgYjVk.png)
阅读全文