【问题标题】:Change the shape of a QLabel更改 QLabel 的形状
【发布时间】:2019-05-05 16:27:35
【问题描述】:

我需要一点帮助,我正在尝试生成一个聊天室,但我希望用于显示消息的 QLabel 具有像这样的消息容器的形式

不仅是 QLabel 的典型方形

我尝试执行以下操作:

def CreateLabel(self):
    image = QtGui.QPixmap("container.png")
    mask = image.createMaskFromColor(QtCore.Qt.red)

    self.Label = QLabel()
    self.Label.setText("Test Text")
    self.Label.setAlignment(QtCore.Qt.AlignRight)
    self.Label.setMask(mask)

【问题讨论】:

    标签: pyqt5


    【解决方案1】:

    试试看:

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui     import *
    from PyQt5.QtCore    import *
    
    
    class Label(QLabel):
        def __init__(self, text):
            super().__init__()
    
            self.text = text
    
            self.im = QImage('D:/_Qt/img/chat.png')
            self.resize(self.im.size())
    
        def paintEvent(self, event):
            super().paintEvent(event)
    
            p = QPainter(self)
            p.drawImage(0, 0, self.im)
    
            self.drawText(event, p)                                 
    
        def drawText(self, event, p):
            p.setPen(QColor(168, 34, 4))
            p.setFont(QFont('Decorative', 12))
            p.drawText(event.rect(), Qt.AlignTop, self.text) 
    
        def closeEvent(self, event):
            quit()
    
    
    class Widget(QWidget):
        def __init__(self):
            super().__init__()
    
            self.text = """
                            How to draw a QLabel with chat image.
                        I would like a little help, I am trying to generate a chat 
                        room but I would like that the QLabel that I use to show 
                        the messages had the form of a message container like this.
                        """        
    
            self.label = Label(self.text)
            self.label.show()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = Widget()
    #    w.show()
        app.exec_()
    

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多