Qt透明背景小工具

时间:2017-05-05 03:09:48

标签: qt transparent

我想制作自己的自定义工具提示。所以我创建了一个类如下。

def __init__(self, parent=None):
    QWidget.__init__(self, parent, Qt.ToolTip)
    self.setAttribute(Qt.WA_TranslucentBackground)

def paintEvent(self, event):
    painter = QPainter(self)
    painter.setRenderHints(QPainter.Antialiasing)
    rect = self.rect()

    path = QPainterPath()
    path.setFillRule(Qt.WindingFill)
    path.addRoundedRect(rect.adjusted(self.PADDING+self.ARROW_DEPTH, self.PADDING, -self.PADDING, -self.PADDING), self.ARROW_DEPTH, self.ARROW_DEPTH)
    path.moveTo(self.PADDING, rect.height()/2)
    path.lineTo(self.PADDING+self.ARROW_DEPTH, rect.height()/2 - self.ARROW_OFFSET)
    path.lineTo(self.PADDING+self.ARROW_DEPTH, rect.height()/2 + self.ARROW_OFFSET)
    path.lineTo(self.PADDING, rect.height()/2)

    painter.setPen(Qt.NoPen)
    painter.setBrush(Qt.red)
    painter.drawPath(path)

但结果是......

Custom tooltip

如何让黑色区域透明?

QWidget.__init__(self, parent, Qt.ToolTip)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)

我添加一行 self.setWindowFlags(Qt.FramelessWindowHint),它可以正常工作。

0 个答案:

没有答案