1

When I try to add a big QString (size:104806123) to a QPlainTextEdit I get an exception :C++: std::bad_alloc

void LogFileialog::add(QString &logLine)
{
    ui.log_plainTextEdit->appendPlainText(logLine);
}

I also tried to append the Text to the QPlainTextEdit splitting the data in many QStrings:

for (int i = 0; i < blockNumber; i++) 
    ui.log_plainTextEdit->appendPlainText(logLine[i]); 

but around at 570.000 row iI get the std::bad_alloc. The problem not is in QString, but when I call appendPlainText(QString)

There is a better way to add a Big String to a QPlainTextEdit ?

Salvo Parisi
  • 119
  • 1
  • 10
  • 1
    Clearly, appending a string involves dynamic memory allocation, which is more likely to fail if allocating a large amount of memory. If there is insufficient memory available and the allocation fails, there isn't a great deal that can be done - other than, maybe (at best), releasing other dynamically allocated memory and trying again. I suggest you redesign your program to avoid needing such a large string, let alone trying to append it. – Peter May 22 '19 at 09:33
  • Generally it is easier to allocate many small blocks of memory than a single large one. See if you can split the data. – Aykhan Hagverdili May 22 '19 at 09:38
  • QString has a limit to its size. I found out the hard way when dealing with 500+ MB xml files in Qt in 64 bit code. – drescherjm May 22 '19 at 11:05
  • https://forum.qt.io/topic/10302/what-is-the-maximum-length-of-string-that-qstring-object-can-hold/4 – drescherjm May 22 '19 at 11:07
  • https://stackoverflow.com/questions/46237476/maximum-size-of-qstring – drescherjm May 22 '19 at 11:12
  • I also tried to append the "logLine" by splitting in blocks : `for (int i = 0; i < blockNumber; i++) ui.log_plainTextEdit->appendPlainText(logLine[i]);` but around at 570.000 row i get the std::bad_alloc. The problem not is in QString, but when I call **appendPlainText(QString)** – Salvo Parisi May 22 '19 at 13:00
  • Because of my three comments I don't think this will work at all regardless of how you attempt. you just have too much data for classes that use `QString` because of the `QString` size limitation. `QPlainTextEdit` likely stores the text in a `QString`. – drescherjm May 22 '19 at 13:09
  • Note that QByteArray does not have the same limitation – drescherjm May 22 '19 at 13:24
  • Ok, as I wrote, I splitted in many QString (1000 char for each String). Now, if I try to add the many QString(s) to QPlainTextEdit, i get the exception around at 570.000 row. – Salvo Parisi May 22 '19 at 13:44
  • Understood, I expect that to happen. – drescherjm May 22 '19 at 20:09
  • Using Qt at 64bit I extended the limit. Now I Can add more than 570.000 rows. I don't know the appendPlainText() 64 bit limit. – Salvo Parisi May 23 '19 at 19:31

0 Answers0