pyqt5 appendPlainText多添加换行符的解决办法

环境:

pyqt5 python3.6

需求:insertplaintext随着内容的增加执行时间越来越长,导致gui卡顿。而appenplaintext没有这个问题,但是会添加一个换行....

这里更正一下:appenplaintext没有卡顿是测试的时间不够长,长时间增加内容测试之后也会卡顿,其实想想也对,每个控件的内容肯定有个极限。对之前的误导表示抱歉。

 

测试代码:

# -*- coding: utf-8 -*-
import sys

from time import clock

from PyQt5.Qt import *
from PyQt5.QtWidgets import *

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.setGeometry(300, 300, 400, 400)
        
        self.pte = QPlainTextEdit(self)
        self.pte.setGeometry(0, 0, 350, 300)

        self.btn = QPushButton('按钮',self)
        self.btn.setGeometry(300, 350, 70 ,30)
        self.btn.clicked.connect(self.on_btn_clicked)
        
        self.cnt = 0

    def on_btn_clicked(self):
        self.pte.moveCursor(QTextCursor.End)
        
        precursor = self.pte.textCursor()
        print('pre'+str(precursor.position()))
        pos = precursor.position()

        self.cnt = self.cnt + 1
        self.pte.appendPlainText(str(self.cnt) + ' ')
        print(self.pte.toPlainText())
        print('pre'+str(precursor.position()))
        
        if pos == 0:
            return
        precursor.setPosition(pos)
        self.pte.setTextCursor(precursor)
        self.pte.textCursor().deleteChar()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Example()
    win.show()
    sys.exit(app.exec_())

注意点:

1、当QPlainTextEdit内容为空时,appendplaintext不自动添加换行

posted @ 2018-04-23 15:12  云中虾  阅读(1425)  评论(0编辑  收藏  举报