Qtextedit VS Qplaintextedit

QTextEdit vs QPlainTextEdit

From Qt's documentation:

QPlainTextEdit is an advanced viewer/editor supporting plain text. It
is optimized to handle large documents and to respond quickly to user
input.

QPlainText uses very much the same technology and concepts as
QTextEdit, but is optimized for plain text handling.

QPlainTextEdit works on paragraphs and characters. A paragraph is a
formatted string which is word-wrapped to fit into the width of the
widget. By default when reading plain text, one newline signifies a
paragraph. A document consists of zero or more paragraphs. Paragraphs
are separated by hard line breaks. Each character within a paragraph
has its own attributes, for example, font and color.

And later on:

Differences to QTextEdit

QPlainTextEdit is a thin class, implemented by using most of the
technology that is behind QTextEdit and QTextDocument. Its performance
benefits over QTextEdit stem mostly from using a different and
simplified text layout called QPlainTextDocumentLayout on the text
document (see QTextDocument::setDocumentLayout()). The plain text
document layout does not support tables nor embedded frames, and
replaces a pixel-exact height calculation with a line-by-line
respectively paragraph-by-paragraph scrolling approach. This makes it
possible to handle significantly larger documents, and still resize
the editor with line wrap enabled in real time. It also makes for a
fast log viewer (see setMaximumBlockCount()).

So the difference is that QPlainTextEdit is optimized for handling plain text, and can be used even with very large plain text files. Also the way text is formatted is simpler.

If you plan to support only plain texts, then QPlainTextEdit is the right choice.

How to make pages look in QTextEdit or QPlainTextEdit

You do not have to replace the existing layout of the QScrollArea, what you have to do is create a widget, and in that widget set the layout. After that widget you must place it in the QScrollArea through the setWidget() method, if you want the height shown to be larger then set a minimum size.

QWidget *contentWidget = new QWidget;

QVBoxLayout *layout =new QVBoxLayout(contentWidget);
ui->scrollArea->setWidget(contentWidget);
ui->scrollArea->setWidgetResizable(true);

QTextEdit *edit = new QTextEdit("hello world");
edit->setMinimumHeight(200);
// create others QTextEdit
layout->addWidget(edit);
// add the QTextEdits

What's a good way to display a lot of text in QT

"QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input."

You can see check detailed information from here.

OpenMP, QTextEdit and QPlainTextEdit

The problem is you are accessing Qt GUI object in a thread other than the main thread.

From http://doc.qt.io/qt-4.8/threads-qobject.html

Although QObject is reentrant, the GUI classes, notably QWidget and
all its subclasses, are not reentrant. They can only be used from the
main thread.

One way around this is to use Qt signals / slots to connect a signal from your worker threads to a slot in your main thread using a QueuedConnection however in your case I do not see this making much sense. Even if you got the signal to work with openmp you will not be able to append strings in a parallel way to QPlainTextEdit that will improve performance.

PySide QTextEdit or QPlainTextEdit update faster?

This has nothing to do with "fast" or "slow".

The GUI runs on the same thread as your listenToServer method - so as long as it's running nothing can happen on the GUI thread. You'll notice that you can't move, resize or click anything in the GUI while you're waiting socket input.

You'll have to run your listenToServer method on a thread separate from the GUI. The proper way to do that would be to implement a Worker object that receives data from the socket and notifies you textEdit via a Signal->Slot connection that there's data ready to receive.

I answered a similar question a while back, that might help
https://stackoverflow.com/a/24821300/2319400

A really quick and dirty alternative would be to process all queued events when you've appended new data, via:

QApplication.processEvents()

This gives Qt time to e.g. repaint the GUI on the screen with new text.
Your GUI will however not respond to any events while python is waiting for data to come from the socket!

QPlainTextEdit display slow performace

I am not sure how helpful my answer will be (most of it is based on personal experience).

Can it be improved with this editor type?

You can not improve performance of setPlainText(), but you can try to improve your highlighting mechanism which will result in better performance.

QPlainTextEdit::setPlainText() is a simple interface to set plain text in your editor but behind the scenes it does some other things such as Syntax highlighting which can noticeably reduce performance because highlightBlock() will be called for every block of text in your file. So if you have 1 million lines in your text file, there will 1 million calls to highlightBlock(). This impacts performance and there is no way to get around this 'easily'.

Most editors I have seen use regular expressions to parse the current text block and then highlight it. A first step towards better performance could be to replace the regular expressions with manual parsing. I have tried this multiple times and it always results in better performance.

One other way, which is rather uncommon, is to use multi-threading for highlighting. You can get all the text in the file and send it to a 'highlighter worker' and let it do the highlighting in the background. You can see this in action here and here.

Is there any other text viewer class that can do it faster?

You can try Scintilla, which is what Notepad++ uses. It can be integerated with Qt C++ and works quite well. There are a few Qt apps that are using it, one example is textosaurus.

Is it possible to create an own text viewer class to improve performance?

Yes, but that will be a monumental task. Read @Vasilij's answer above for this.

QLabel vs QTextEdit, to show long text. Which is faster?

In case you don't have rich text and want to only show plain text, it's better to use QPlainTextEdit which is exactly for showing large texts. It uses most of the features of QTextEdit but with a vastly better performance.

Using QPlainTextEdit is better than using QLabel for displaying large text documents as the former has many more capabilities and features for customization and formatting.

How to setText for QPlainTextEdit?

It's very simple, just get the current document and set its text:

plain_text_edit->document()->setPlainText(text);

Alternative way, just call this method:

plain_text_edit->setPlainText(text);

You could also use text cursor of the editor in many ways to achieve this, most simply by selecting entire existing text (assuming the editor is not empty), then doing plain_text_edit->TextCursor().insertText(text); (which replaces currently selected text with usual paste semantics), but for the simple case of replacing all text, that's overcomplicated.

QPlainTextEdit for single line of input

Take a look at the QLineEdit class.



Related Topics



Leave a reply



Submit