QLabel自动换行的实现

我试的,自带就可以换行,

#! /usr/bin/python3
# coding = utf-8

import sys

from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QWidget()
    w.resize(250, 150)
    w.move(300,300)

    dlgLayout = QVBoxLayout()
    dlgLayout.setContentsMargins(40, 40, 40, 40)
    btn = QLabel('测试11111\n111111111111111按钮')

    # btn.adjustSize()

    # btn.setGeometry(QRect(32, 40, 59, 27*4 )); # 四倍行距
    # btn.setWordWrap(True);
    # btn.setAlignment(Qt::AlignTop);
    dlgLayout.addWidget(btn)
    w.setLayout(dlgLayout)
    w.setWindowTitle('Hello world')
    w.show()
    sys.exit(app.exec_())


如果不可以,就参考这个试试: 

https://blog.csdn.net/wukai_std/article/details/62893829

1. 让QLabel自适应text的大小,直接用下面的代码: 
LabelName->adjustSize(); 
2. 让QLabel能够自动判断并换行显示: 
LabelName->setGeometry(QRect(328, 240, 329, 27*4)); //四倍行距 
LabelName->setWordWrap(true); 
LabelName->setAlignment(Qt::AlignTop); 
这种方法如果全是ASCII字符并超过一行,则不会换行! 
╮(╯▽╰)╭只能自己动手了

# 下面的代码实现了聊天气泡大小自适应于内容,解决了中文与ASCII字符大小不同以及ASCII字符过长
# 不自动换行的问题
height = 0
width = 0
height = 56
checkLengthOver = 0
chineseExistFlag = 0
textSlice = sendText.split("\n")
sendTextList = list()

for s in textSlice:
    tmpText = ""
    tmpNum = 0
    tmpWidth = 0
    tmpNumList = list()
    tmpNumList.append(0)
    status = 0
    if not s:
        height += 16
    for i in s:
        if ord(i) in range(257):
            tmpWidth += 600 / 73
            tmpNum += 1
            if not status:
                status = 1
                height += 16
        else:
            tmpWidth += 600 / 44
            tmpNum += 1
            if not status:
                status = 1
                height += 16.3
            chineseExistFlag = 1

        if tmpWidth > 600 - 600 / 44:
            tmpNumList.append(tmpNum)
            checkLengthOver = 1
            tmpWidth = 0
            if chineseExistFlag:
                chineseExistFlag = 0
                height += 16.3
            else:
                height += 16

        if tmpWidth > width:
            width = tmpWidth
        if checkLengthOver == 1:
            width = 600

    for j in range(len(tmpNumList)-1):
        tmpText = tmpText + s[tmpNumList[j]:tmpNumList[j+1]] + "\n"
    tmpText = tmpText + s[tmpNumList[-1]:]
    sendTextList.append(tmpText)
sendText = "\n".join(sendTextList)
            # sendText = "

%s

"%(90, sendText)
if width == 0: width = 70 else: width += 80 itemHeight = height + 100 self.item.lbSendText.setMinimumSize(QtCore.QSize(width, height)) self.item.lbSendText.setMaximumSize(QtCore.QSize(width, height)) self.item.lbSendText.setWordWrap(True) self.item.lbSendText.setText(sendText) self.item.lbSendText.adjustSize() self.item.lbSendText.setScaledContents(True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

代码不太全,有些参数定义没去找了,但细心推导还是能推出来的。


你可能感兴趣的:(pyqt)