void MyEditor :: highlight(const QString& txt)
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (txt.isEmpty()) {
setExtraSelections(extraSelections);
return;
}
while (find(txt, flags)) {
extra.format.setBackground(color);
extra.cursor = textCursor();
extraSelections.append(extra);
}
setExtraSelections(extraSelections);
}
void MyEditor :: highlight(const QString& txt)
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (txt.isEmpty()) {
setExtraSelections(extraSelections);
return;
}
QTextDocument::FindFlags flags = ...;
QColor color(Qt::lightGray);
while (find(txt, flags)) {
QTextEdit::ExtraSelection extra;
extra.format.setBackground(color);
extra.cursor = textCursor();
extraSelections.append(extra);
}
setExtraSelections(extraSelections);
}
To copy to clipboard, switch view to plain text mode
void MyEditor
:: highlight(const Qtring
& txt,
QTextDocument::FindFlags flags,
int start,
int end
) {
if (document()) {
QList<QTextEdit::ExtraSelection> extraSelections;
cursor.setPosition(start);
cursor = document() -> find(txt, cursor, flags);
while (! cursor.isNull()) {
if (cursor.position() > end) {
break;
}
extra.format.setBackground(color);
extra.cursor = cursor;
extraSelections.append(extra);
cursor = document() -> find(txt, cursor, flags);
}
setExtraSelections(extraSelections);
}
}
void MyEditor :: updateHighlight(const QRect& rect, int dy)
{
if (dy != 0) {
if (isHighlightEnabled) {
//------------------------------------------------------------------------------
// update only the visible part
//------------------------------------------------------------------------------
QPoint pageY
= viewport
() ?
QPoint(viewport
() -> width
() - 1, viewport
() -> height
() - 1) : QPoint(0,
0);
highlight(txt, findFlags, cursor.position(), cursorForPosition(pageY).position());
}
}
}
{
connect(this, SIGNAL(updateRequest(const QRect&, int)), SLOT(updateHighlight(const QRect&, int)));
}
void MyEditor :: highlight(const Qtring& txt, QTextDocument::FindFlags flags, int start, int end)
{
if (document()) {
QList<QTextEdit::ExtraSelection> extraSelections;
QColor color(Qt::lightGray);
QTextCursor cursor(document());
cursor.setPosition(start);
cursor = document() -> find(txt, cursor, flags);
while (! cursor.isNull()) {
if (cursor.position() > end) {
break;
}
QTextEdit::ExtraSelection extra;
extra.format.setBackground(color);
extra.cursor = cursor;
extraSelections.append(extra);
cursor = document() -> find(txt, cursor, flags);
}
setExtraSelections(extraSelections);
}
}
void MyEditor :: updateHighlight(const QRect& rect, int dy)
{
if (dy != 0) {
if (isHighlightEnabled) {
//------------------------------------------------------------------------------
// update only the visible part
//------------------------------------------------------------------------------
QTextCursor cursor = cursorForPosition(QPoint(0, 0));
QPoint pageY = viewport() ? QPoint(viewport() -> width() - 1, viewport() -> height() - 1) : QPoint(0, 0);
highlight(txt, findFlags, cursor.position(), cursorForPosition(pageY).position());
}
}
}
MyEditor :: MyEditor(QObject* p)
{
connect(this, SIGNAL(updateRequest(const QRect&, int)), SLOT(updateHighlight(const QRect&, int)));
}
To copy to clipboard, switch view to plain text mode
Bookmarks