[Qt-interest] Hightlight Text in QTextEdit
Phoenix.Bot
phoenix.god at gmx.de
Mon Jul 11 21:41:11 CEST 2011
Thanks for the Suggestion, however it does not work...
I was unable to locate examples but I tried the following approach with the
ExtraSelections:
void highlightSpelling() {
QTextDocument *pDoc = ui->txtNewMsg->document();
QTextCharFormat eFmt;
QTextCharFormat dFmt;
eFmt.setUnderlineColor(Qt::red);
eFmt.setUnderlineStyle(QTextCharFormat::WaveUnderline);
dFmt.setUnderlineStyle(QTextCharFormat::NoUnderline);
dFmt.setToolTip("");
QTextEdit::ExtraSelection es;
QTextCursor cur(pDoc);
cur.movePosition(QTextCursor::Start);
QStringList parts = pDoc->toPlainText().split(" ",
QString::SkipEmptyParts);
int lastCurIndex = 0;
for (int i = 0; i < parts.count(); ++i) {
QApplication::processEvents();
if (lstWords.count() >= i+1 &&
lstWords.at(i).compare(parts.at(i)) == 0) {
qDebug() << "checked before. Skip";
continue;
}
if (parts.at(i).length() < 2) {
qDebug() << "shorter than 2 Characters. Skip";
continue;
}
cur.movePosition(QTextCursor::Right,
QTextCursor::MoveAnchor, pDoc->toPlainText().indexOf(parts.at(i),
lastCurIndex));
cur.movePosition(QTextCursor::Right,
QTextCursor::KeepAnchor, parts.at(i).length());
lastCurIndex = cur.position();
qDebug() << "Cursor has the following Selection: " <<
cur.selectedText();
qDebug() << "Checking Word " << parts.at(i);
if (!isWordOK(parts.at(i))) {
eFmt.setToolTip(getSuggestions(parts.at(i)).join(",
"));
es.format = eFmt;
//cur.setCharFormat(eFmt);
qDebug() << "Bad Spelling.";
} else {
es.format = dFmt;
//cur.setCharFormat(dFmt);
}
es.cursor = cur;
QApplication::processEvents();
QList<QTextEdit::ExtraSelection> esL =
ui->txtNewMsg->extraSelections();
esL.append(es);
ui->txtNewMsg->setExtraSelections(esL);
}
lstWords = parts;
}
It does the Format the first time but when the function runs again and as
such adds another ExtraSelection to the list it crashes on
setExtraSelections().
As you can see in the above Code I also tried to use
QTextCursor::setCharFormat which works fine for all words, but it causes the
onTextChanged() Signal to be fired, on which I call above Function.
-----Original Message-----
From: Diego Iastrubni [mailto:diegoiast at gmail.com]
Sent: Montag, 11. Juli 2011 18:54
To: Phoenix.Bot
Cc: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Hightlight Text in QTextEdit
Use setExtraSelections(). There are demos for this, and if I am wrong just
ask again. :-)
More information about the Qt-interest-old
mailing list