[英]Is it possible to render text as being “cut out” from a layer in in qt5?
您可以使用QPainterPath
和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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.