【发布时间】:2020-11-10 22:24:59
【问题描述】:
如何使用 PyQt5 将多个点与流动曲线连接起来?例如,我尝试使用 quadTo() 对 8 个点执行此操作,使用交替点作为控制点,但弧线不接触控制点(参见下面的代码和图表)。我也尝试使用cubicTo(),但这也导致了一条奇怪的曲线。是否有任何其他我应该使用的函数调用,或者自定义的方法来做到这一点?
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "PyQt5 Drawing Tutorial"
self.top= 150
self.left= 150
self.width = 500
self.height = 500
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
def paintEvent(self, event):
painter = QPainter(self)
path = QPainterPath()
points = [
QPoint(20,40),
QPoint(60,10),
QPoint(100,50),
QPoint(80,200),
QPoint(200,300),
QPoint(150,400),
QPoint(350,450),
QPoint(400,350),
]
# draw small red dots on each point
painter.setPen(QtCore.Qt.red)
painter.setBrush(QBrush(Qt.red))
for i in range(len(points)):
painter.drawEllipse(points[i], 3, 3)
painter.setPen(QtCore.Qt.blue)
painter.setBrush(QBrush(Qt.red, Qt.NoBrush)) #reset the brush
path.moveTo(points[0])
# connect the points with blue straight lines
#for i in range(len(points)-1): # 1 less than length
# path.lineTo(points[i+1])
# connect points with curve
for i in range(0,len(points),2):
path.quadTo(points[i], points[i+1])
painter.drawPath(path)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
【问题讨论】:
-
您能否阐明您想要实现的目标,可能使用显示预期结果的图像?我的印象是您想绘制一条连接所有点的“平滑”曲线,但在这种情况下: 1. 这是一个数学问题; 2. 有无数种方法可以做到这一点;
-
第 2 项 - 任何连接所有点的平滑曲线现在都可以。关于第 1 项 - 是否有可以在 pyqt5 框架内调用的函数?