是否可以绘制一个不单调但带有渐变的 QPolygon。我的代码:
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtGui import QPen,QPainter, QPolygon, QRadialGradient, QBrush, QColor
from PyQt5.QtCore import QPoint, Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "PyQt5 Drawing Polygon"
self.width = 900
self.height = 900
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(0,0, self.width, self.height)
self.show()
def paintEvent(self, event):
painter = QPainter(self)
gradient = QRadialGradient(50, 50, 50, 50, 50)
gradient.setColorAt(0, QColor.fromRgbF(100, 125, 0, 0.5))
gradient.setColorAt(1, QColor.fromRgbF(0, 255, 0, 0.5))
brush = QBrush(gradient)
pen = QPen(brush, 50, Qt.SolidLine)
pen.setJoinStyle(Qt.RoundJoin)
pen.setCapStyle(Qt.RoundCap)
painter.setPen(pen)
points = QPolygon([
QPoint(80,80),
QPoint(80,500),
QPoint(500,80),
QPoint(500,500)
])
painter.drawPolyline(points)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
所需结果的示例(如果可能的话......):
我没有想到比通过循环绘制所有内容更好的方法,只是使用大量 QPen 并进行最小的颜色变化,这样过渡就不会可见,而且看起来像渐变。