使QTextEdit大小适合文本大小pyqt5

时间:2019-05-23 20:02:43

标签: python python-3.x pyqt pyqt5 qtextedit

我尝试使QTextEdit将其宽度值更改为在其中输入的文本的长度。

但是问题是,当使用resize属性时,它什么也不做,并且不会更改大小

我遵循列表中当前单词的长度,并且该值是我尝试将其作为属性width()发送给QTextEdit的值

得到这样的东西:

from PyQt5.QtWidgets import QMainWindow,QWidget,QVBoxLayout,QApplication,QTextEdit,QPushButton,QScrollArea

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.lista = ["one","two","abcdefghijklmn","zxyw","xyxyxyxyx"]

        self.widget = QWidget(self)
        self.layout = QVBoxLayout(self.widget)

        self.area = QScrollArea(self)
        self.area.resize(400,300)
        self.area.setWidget(self.widget)
        self.area.setWidgetResizable(True)

        self.plain  =QTextEdit(self)
        self.plain.move(0,305)
        self.plain.resize(400,50)

        self.boton = QPushButton(self)
        self.boton.move(0,360)
        self.boton.setText("Press")

        self.boton.clicked.connect(self.Test)

    def Test(self):
        for i in self.lista:
            longitud = len(i)*6.3
            print(longitud)
            self.text = QTextEdit(self)
            self.text.document().setPlainText(i)
            self.text.setReadOnly(True)
            self.text.resize(longitud,10)
            self.layout.addWidget(self.text)

app = QApplication([])
m = Main()
m.show()
m.resize(600,400)
app.exec()

实际上我需要的是创建用来填充QScrollArea的QTextEdit符合文本字符长度的大小

enter image description here

这是我得到的结果,但我需要的是QTextEdit具有行尾的width()

1 个答案:

答案 0 :(得分:1)

这是我尝试使用字体指标来测量文本框内容大小的方法来解决此问题:

import sys

from PyQt5.QtGui import QFontMetrics
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QApplication, QTextEdit, QPushButton, QScrollArea


class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.lista = ["one", "two", "abcdefghijklmn", "zxyw", "xyxyxyxyx"]

        self.widget = QWidget(self)
        self.layout = QVBoxLayout(self.widget)

        self.area = QScrollArea(self)
        self.area.resize(400,300)
        self.area.setWidget(self.widget)
        self.area.setWidgetResizable(True)

        self.plain = QTextEdit(self)
        self.plain.move(0,305)
        self.plain.resize(400,50)

        self.boton = QPushButton(self)
        self.boton.move(0,360)
        self.boton.setText("Press")

        self.boton.clicked.connect(self.Test)

    def Test(self):
        for i in self.lista:
            text = QTextEdit(self)
            text.document().setPlainText(i)

            font = text.document().defaultFont()
            fontMetrics = QFontMetrics(font)
            textSize = fontMetrics.size(0, text.toPlainText())

            w = textSize.width() + 10
            h = textSize.height() + 10
            text.setMinimumSize(w, h)
            text.setMaximumSize(w, h)
            text.resize(w, h)

            text.setReadOnly(True)

            self.layout.addWidget(text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    m = Main()
    m.show()
    m.resize(600, 400)
    sys.exit(app.exec_())

结果:

Result