PDA

View Full Version : QTextCursor manipulation truncates my text content in QTextEdit !?!?!



Thomas G2
18th October 2013, 13:03
Dear colleagues,
does anybody know about limits of text content in QTextEdit or QTextBrowser?
I would like to show 500k of text (5000 lines of 100chars per line) to the user and do some simple content manipulation (like inserting some snippets or set background colors for specific words). But as soon as this kind of adaption occurs, my text will be truncated to 154 lines.
Has anybody seen this before?

Env: Qt5.1.1, Win8, VS2012

Here is a small code to reproduce this anomaly.



// create the UI
QDialog *dial = new QDialog;
dial->resize(400, 300);
QVBoxLayout *vbl = new QVBoxLayout(dial);
QTextEdit* edit = new QTextEdit();
vbl->addWidget(edit);
edit->setLineWrapMode(QTextEdit::NoWrap);
edit->setAcceptRichText(false);
dial->setLayout(vbl);
dial->show ();

// add some content text
QString s;
for (int i=0; i<5000; i++)
s += QString::number(i) + ": " + QString('x',120) + "\n";
edit->setPlainText (s);

// now lets manipulate the text. e.g. prepand "Start"
QTextCursor cursor = edit->textCursor();
cursor.movePosition(QTextCursor::Start);
cursor.insertText ("Start: "); // this command truncates the content to 154 lines
// if you comment this out, the whole content will be shown

Thank you in advance.
Regards, Thomas

Added after 42 minutes:

I found in another question the source of the problem.
edit->setPlainText (and I think also the other variants) does it's job in parts of text in a kind of background tasks. Therefore I see only the first chunk of text.
When I add a QApplication::processEvents (QEventLoop::AllEvents, 2000); it shows the complete text. It's only a workaround and not a real solution. A better way would be to do manipulation after a "finished" signal from the QTextEdit. But I don't see one. I only found "textChanged" and I think this will be fired with every little changing and not only by this initial setting.
Do you have any other idea?
Best regards,
Thomas