创建具有渐变颜色的 pyqt5 文本

Creating a pyqt5 text with a gradient color

我想创建一个渐变文本。我找到了创建渐变背景但不是文本的方法。

创建一个带有渐变的画笔作为画笔

from PyQt5 import QtWidgets, QtCore, QtGui

class Widget(QtWidgets.QWidget):
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        font = QtGui.QFont("Arial", 72)
        painter.setFont(font)
        rect = self.rect()
        gradient = QtGui.QLinearGradient(rect.topLeft(), rect.topRight())
        gradient.setColorAt(0, QtCore.Qt.red)
        gradient.setColorAt(1, QtCore.Qt.blue)
        pen = QtGui.QPen()
        pen.setBrush(gradient)
        painter.setPen(pen)
        painter.drawText(QtCore.QRectF(rect), "Hello world", QtGui.QTextOption(QtCore.Qt.AlignCenter))

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    widget = Widget()
    widget.show()
    app.exec()