Exemplo n.º 1
0
    def __init__(self, parent=None, text="", norImage=None, preImage=None):
        super(NinePatchLabel, self).__init__(text, parent)
        self.norImage = norImage
        self.preImage = preImage
        self.norNinePatch = NinePatch(norImage)
        self.preNinePatch = NinePatch(preImage)
        self.curNinePatch = self.norNinePatch

        if norImage and preImage:
            minWidth = min(self.norNinePatch.width(),
                           self.preNinePatch.width())
            minHeight = min(self.norNinePatch.height(),
                            self.preNinePatch.height())
        elif norImage:
            minWidth = self.norNinePatch.width()
            minHeight = self.norNinePatch.height()
        self.setWordWrap(True)
        self.adjustSize()
        self.resize(minWidth, minHeight)
Exemplo n.º 2
0
class NinePatchLabel(QLabel):

    clicked = pyqtSignal(str)  # 点击信号

    def __init__(self, parent=None, text="", norImage=None, preImage=None):
        super(NinePatchLabel, self).__init__(text, parent)
        self.norImage = norImage
        self.preImage = preImage
        self.norNinePatch = NinePatch(norImage)
        self.preNinePatch = NinePatch(preImage)
        self.curNinePatch = self.norNinePatch

        if norImage and preImage:
            minWidth = min(self.norNinePatch.width(),
                           self.preNinePatch.width())
            minHeight = min(self.norNinePatch.height(),
                            self.preNinePatch.height())
        elif norImage:
            minWidth = self.norNinePatch.width()
            minHeight = self.norNinePatch.height()
        self.setWordWrap(True)
        self.adjustSize()
        self.resize(minWidth, minHeight)

    def __del__(self):
        if hasattr(self, "ninepatch"):
            del self.ninepatch

    def mousePressEvent(self, event):
        self.clicked.emit(self.text())
        super(NinePatchLabel, self).mousePressEvent(event)
        self.curNinePatch = self.preNinePatch  # 按下鼠标的效果
        if self.norImage and self.preImage:
            self.update()

    def mouseReleaseEvent(self, event):
        super(NinePatchLabel, self).mouseReleaseEvent(event)
        self.curNinePatch = self.norNinePatch  # 释放鼠标的效果
        if self.norImage and self.preImage:
            self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        try:
            self.curNinePatch.SetImageSize(self.width(), self.height())
            self.curNinePatch.Draw(painter, 0, 0)
        except Exception as e:
            print(e)
        super(NinePatchLabel, self).paintEvent(event)