2

I am experimenting with QPlaintextedit widget as a text editor, so far it works great, using it I can type large amounts of text and the UI doesn't freeze or stutter. I thought I would push the boundaries and see what happens.

The basic gist is that using my editor i can write pseudo code, and then parse the code for mistakes. If there are no mistakes the parse spits out some xml based on the input text. In the end I get a nice xml document describing the text. Essentially I have managed to transform pseudo code into an xml file.

This works reasonably well, but the more text in the editor the more memory it uses. Now I managed to paste about 750k lines of text into my editor, when it came time to parse it, I first read the text and then send the entire text to the parser. To that end I do:

editor_text=QPlainTextEdit.toPlainText()

This gives me all the text in the editor which I can send to the parser and then convert it to and xml file (if no mistakes are found)

Now with 750k lines of text in the editor the toPlainText() method doesn't work so well, in fact I just run out of memory.

My question is how should I deal with really large amounts of text in order to parse it.

One thing I have thought about (not tried) is reading the text block by block (or line by line) parsing each line and converting it to xml, but I'd still have to deal with the returned xml, keeping the resulting xml for each line/block in memory until the entire editor text has been parsed would still probably run out of memory

I cannot imagine that this is just to do with QPlainTextEdit widget, but in general when there is a large amount of "code"/text say 1M, or even 10M lines of "code", in a single file how would one go about reading and parsing all 10M lines?

for my example I'm using python 2.7 on Windows with pyqt4.8

user595985
  • 1,543
  • 4
  • 29
  • 55
  • 3
    Have you tried using a cursor to read it in smaller chunks? Perhaps if you read 10k or 100k lines each time it would work. – Photon Sep 14 '16 at 20:56
  • does it crash if you don't try to parse it? – Jean-François Fabre Sep 14 '16 at 21:00
  • Is this all just hypothetical? Why would you ever need to load such a large amount of text? Code files are usually about a few hundred kb, and only very rarely more than a few mb. Personally, I wouldn't waste any effort trying to solve this "problem". – ekhumoro Sep 14 '16 at 21:32
  • @ekhumoro, yeah i'm trying it well because I can, generally I can't see this happening much, but I can copy paste so I did, and I did it until something broke so I thought I'd ask for a general way of looking at the problem. It is mainly hypothetical, but if someone has a clever idea I'm happy to read it and maybe try it out. Thanks – user595985 Sep 15 '16 at 05:32
  • @user595985. It's unclear from your question what you're really asking. The only really *specific* problem that I can see here, is the claim that calling `toPlainText()` can cause an out of memory error. If true, that would seem to be a bug in Qt. In which case, you should provide a [mcve] that demonstrates the problem so that others can try to reproduce it. – ekhumoro Sep 15 '16 at 13:31

0 Answers0