Reputation: 2695
I need to paint some words in QTextEdit by other colors.
void Wnd::onTextChanged()
{
QString s = ui->textEdit->toPlainText();
s = addColors(s);
ui->textEdit->blockSignals(true);
ui->textEdit->setHtml(s);
ui->textEdit->blockSignals(false);
}
QTextEditor::setHtml
set cursor and visible region in the beginning. But I need to my QTextEdit
using will be no different from simple QTextEdit
using.
Upvotes: 0
Views: 608
Reputation: 38161
You are doing that wrong, creating big overhead. You should do this like that:
void Wnd::onTextChanged()
{
QTextDocument *doc = ui->textEdit->document();
// clears old formating
QTextCursor cursor(doc);
cursor.select(QTextCursor::Document);
cursor.setCharFormat(QTextCharFormat());
Q_FOREACH(QString word, wordsToColor) {
QTextCursor cursor = doc->find(word);
while(cursor.hasSelection()) {
cursor.setCharFormat(someTextCharFormat);
cursor = doc->find(word, cursor); // next word
}
}
}
It is also possible that you are trying to do something what is already solved: http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter.html
Upvotes: 2