Minimal example:
#include <QApplication>
#include <QPlainTextEdit>
#include <QSyntaxHighlighter>
public:
{
}
void highlightBlock
(const QString &text
){ myClassFormat.
setFontWeight(QFont::Bold);
myClassFormat.
setFontCapitalization(QFont::AllUppercase);
myClassFormat.setForeground(Qt::darkMagenta);
int index = text.indexOf(expression);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, myClassFormat);
index = text.indexOf(expression, index + length);
}
}
};
int main(int argc, char *argv[])
{
QPlainTextEdit textEdit;
dummyHighlighter highlighter(textEdit.document());
textEdit.show();
return a.exec();
}
#include <QApplication>
#include <QPlainTextEdit>
#include <QSyntaxHighlighter>
class dummyHighlighter : public QSyntaxHighlighter{
public:
dummyHighlighter(QTextDocument *parent):
QSyntaxHighlighter(parent)
{
}
void highlightBlock(const QString &text){
QTextCharFormat myClassFormat;
myClassFormat.setFontWeight(QFont::Bold);
myClassFormat.setFontCapitalization(QFont::AllUppercase);
myClassFormat.setForeground(Qt::darkMagenta);
QString pattern = "(input)";
QRegExp expression(pattern);
int index = text.indexOf(expression);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, myClassFormat);
index = text.indexOf(expression, index + length);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPlainTextEdit textEdit;
dummyHighlighter highlighter(textEdit.document());
textEdit.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The word input becomes bold and purple but doesn't turn to uppercase.
Bookmarks