[PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function
-
Hi everyone,
Here is my problem.
I'm writing a Python application with PySide2. In the code, I open a text file. The text file has 160k lines. I did some operations on each line and concatenated each line together to a very long string. Each line was separated by "\n". I call QTextEdit.setPlainText() to set the very long string into the QTextEdit.
It is very slow when scrolling the scroll bar or even got "not responding" eventually.Could you please give me any suggestions to speed it up or solve the problem?
Thanks a lot!
Rex -
Hi everyone,
Here is my problem.
I'm writing a Python application with PySide2. In the code, I open a text file. The text file has 160k lines. I did some operations on each line and concatenated each line together to a very long string. Each line was separated by "\n". I call QTextEdit.setPlainText() to set the very long string into the QTextEdit.
It is very slow when scrolling the scroll bar or even got "not responding" eventually.Could you please give me any suggestions to speed it up or solve the problem?
Thanks a lot!
Rex@Rex-Li
Tested the following code under Ubuntu, Qt 5.1.5.x, PySide2.import sys from PySide2 import QtCore, QtWidgets # from PyQt5 import QtWidgets def window(): app = QtWidgets.QApplication(sys.argv) w = QtWidgets.QPlainTextEdit() w.resize(800, 800) f = QtCore.QFile("plaintextfile.txt") f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text) bs = bytes(f.readAll()) txt = bs.decode('utf-8') w.setPlainText(txt) f.close() w.show() sys.exit(app.exec_()) if __name__ == '__main__': window()
File is 180k lines, 6.5MB size. Reads, displays and scrolls immediately and perfectly.
Suggest you start by removing your own processing and checking above with your original 160k file? (If you have done something wrong about decoding/newlines you might have the whole thing as a single line/bytes, that might be an issue....
-
@JonB
Thanks for your reply!
I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
Then it has the same symptom.
It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.Did I use the QMainWindow in the wrong way?
Thanks a lot!
The following is my code.import sys from PySide2 import QtCore, QtWidgets # from PyQt5 import QtWidgets def window(): app = QtWidgets.QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_()) class MyWindow(QtWidgets.QMainWindow): def __init__(self,parent=None): super().__init__(parent) self.te = QtWidgets.QTextEdit() self.setCentralWidget(self.te) self.read_file() def read_file(self): f = QtCore.QFile("abc.txt") f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text) bs = bytes(f.readAll()) txt = bs.decode('utf-8') self.te.setPlainText(txt) f.close() if __name__ == '__main__': window()
-
@JonB
Thanks for your reply!
I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
Then it has the same symptom.
It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.Did I use the QMainWindow in the wrong way?
Thanks a lot!
The following is my code.import sys from PySide2 import QtCore, QtWidgets # from PyQt5 import QtWidgets def window(): app = QtWidgets.QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_()) class MyWindow(QtWidgets.QMainWindow): def __init__(self,parent=None): super().__init__(parent) self.te = QtWidgets.QTextEdit() self.setCentralWidget(self.te) self.read_file() def read_file(self): f = QtCore.QFile("abc.txt") f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text) bs = bytes(f.readAll()) txt = bs.decode('utf-8') self.te.setPlainText(txt) f.close() if __name__ == '__main__': window()
@Rex-Li said in [PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function:
self.te = QtWidgets.QTextEdit()
@JonB used QPlainTextEdit - did you try that?
-
@JonB
Thanks for your reply!
I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
Then it has the same symptom.
It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.Did I use the QMainWindow in the wrong way?
Thanks a lot!
The following is my code.import sys from PySide2 import QtCore, QtWidgets # from PyQt5 import QtWidgets def window(): app = QtWidgets.QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_()) class MyWindow(QtWidgets.QMainWindow): def __init__(self,parent=None): super().__init__(parent) self.te = QtWidgets.QTextEdit() self.setCentralWidget(self.te) self.read_file() def read_file(self): f = QtCore.QFile("abc.txt") f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text) bs = bytes(f.readAll()) txt = bs.decode('utf-8') self.te.setPlainText(txt) f.close() if __name__ == '__main__': window()
@Rex-Li
Without even rewriting my code to useQMainWindow
, if you try it withQTextEdit
instead ofQPlainTextEdit
then yes scrolling etc. gets very "sticky". I had not noticed you had written "QTextEdit with QTextEdit.setPlainText() function". Assuming your text is plain text, looks like you need to useQPlainTextEdit
for speed. -
@Rex-Li said in [PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function:
self.te = QtWidgets.QTextEdit()
@JonB used QPlainTextEdit - did you try that?
-
@Rex-Li
Without even rewriting my code to useQMainWindow
, if you try it withQTextEdit
instead ofQPlainTextEdit
then yes scrolling etc. gets very "sticky". I had not noticed you had written "QTextEdit with QTextEdit.setPlainText() function". Assuming your text is plain text, looks like you need to useQPlainTextEdit
for speed.