簡體   English   中英

是否可以將文本渲染為從 qt5 中的圖層“剪切”?

[英]Is it possible to render text as being “cut out” from a layer in in qt5?

我是ui設計的新手,我想知道如何實現這樣的效果,即圖層頂部的一些基於字體的文本被繪制為從所述圖層“剪切”,如果這可能的話。

概念

Basically akin to the example below理想情況下,從其中“剪切”文本的圖層本身可以具有效果,例如半透明和/或模糊,如果所述圖層具有跨背景的翻譯,效果仍然有效。

例子

您可以使用QPainterPathQPainter來做到這一點。

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM