在qt5中,是否可以将文字从图层中 "切 "出来?

问题描述 投票:0回答:1

我是个新手 ui 我想知道如何实现一个效果,即在一个图层上的一些基于字体的文字被绘制成从上述图层中 "切出 "的效果,如果这是有可能的话。

concept

Basically akin to the example below 理想情况下,文字所在的图层是 "剪掉" 的效果,比如半透明或模糊,如果该图层在背景上有一个转换,效果仍然有效。

example

fonts pyqt5 qt5 alphablending
1个回答
2
投票

你可以用 QPainterPath 和a QPainter.

使用 QPainterPath 来定义图像上的白色部分。首先,用一个矩形(圆角矩形)创建一个路径。然后,用正确的字体将你的文本添加到路径中。默认情况下,填充规则将从矩形中移除文本。

使用 QPainter 来绘制背景图片上的路径。不要忘记启用抗锯齿以获得更好的渲染效果。

下面是一个例子。

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        label = QLabel(self)
        label.setPixmap(self.makePixmap("knockout"))

    def makePixmap(self, text):
        background = QPixmap(600, 80)
        background.fill(Qt.red)      # Your background image

        textMask = QPainterPath() 
        textMask.addRect(75, 20, 300, 40) # The white part
        textMask.addText(QPoint(90, 50), QFont("Helvetica [Cronyx]", 24), text) # the path will substract the text to the rect

        painter = QPainter(background)
        painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)

        painter.fillPath(textMask, QBrush(Qt.white)) # Will draw the white part with the text "cut out"

        painter.end()
        return background
© www.soinside.com 2019 - 2024. All rights reserved.