QSyntaxHighlighter class provides a mechanism for defining highlighting rules.
More...
|
| QSyntaxHighlighter (QObject *parent) |
|
| QSyntaxHighlighter (QTextDocument *parent) |
|
virtual | ~QSyntaxHighlighter () |
|
QTextDocument * | document () const |
|
void | setDocument (QTextDocument *doc) |
|
| QObject (QObject *parent=nullptr) |
|
| ~QObject () |
|
bool | blockSignals (bool block) |
|
const QList< QObject * > & | children () const |
|
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
|
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
|
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
|
void | dumpObjectInfo () |
|
void | dumpObjectTree () |
|
QList< QString > | dynamicPropertyNames () const |
|
virtual bool | event (QEvent *event) |
|
virtual bool | eventFilter (QObject *watched, QEvent *event) |
|
template<typename T > |
T | findChild (const QString &childName=QString ()) const |
|
template<class T > |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
|
template<class T > |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
|
bool | inherits (const QString &className) const |
|
void | installEventFilter (QObject *filterObj) |
|
bool | isWidgetType () const |
|
bool | isWindowType () const |
|
void | killTimer (int id) |
|
const QMetaObject * | metaObject () const |
|
void | moveToThread (QThread *targetThread) |
|
QString | objectName () const |
|
QObject * | parent () const |
|
template<class T = QVariant> |
T | property (const QString &name) const |
|
void | removeEventFilter (QObject *obj) |
|
void | setObjectName (const QString &name) |
|
void | setParent (QObject *parent) |
|
bool | setProperty (const QString &name, const QVariant &value) |
|
bool | signalsBlocked () const |
|
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
|
QThread * | thread () const |
|
|
void | destroyed (QObject *obj=nullptr) |
|
void | objectNameChanged (const QString &objectName) |
|
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
|
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
|
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
|
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
|
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
|
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
|
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
|
static QMetaObject & | staticMetaObject () |
|
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
|
| objectName |
|
T | qobject_cast (QObject *object) |
|
| QObjectList |
|
The QSyntaxHighlighter class provides a mechanism for defining highlighting rules. This abstract class was designed to be used as a base class in a user application. To implement syntax highlighting inherit from QSyntaxHighlighter and implement the virtual highlightBlock() method. In the constructor of your new class, pass a pointer to the QTextDocument which contains the text to be highlighted.
Example 1
In this example a new class called MySyntax is declared which must inherit from QSyntaxHighlighter and override the highlightBlock() method.
{
CS_OBJECT(MySyntax)
public:
protected:
};
The highlightBlock() method contains the rules to configure the font and coloring for a given word located in the text. This method must call the base class method setFormat() to actually apply the formatting changes.
void MySyntax::highlightBlock(
const QString &text) {
QString pattern =
"\\bMy[A-Za-z]+\\b";
while (iter != text.end()) {
}
}
To use the MySyntax class for highlighting create a widget like a QTextEdit or QPlainTextEdit. The widget data type must be able to return a pointer to a QTextDocument.
MySyntax *highlighter = new MySyntax(editor->document());
Multi Line Highlighting
There are cases where highlighting will need to span multiple text blocks. An example would be a multiline comment in C++. This requires the syntax highlighter to store the end state of the previous text block. The end state of the previous text block can be retrieved by calling previousBlockState(). After parsing the block save the last state using setCurrentBlockState().
The currentBlockState() and previousBlockState() methods return an integer value. If no state is set the returned value is -1. Other values can be used to indicate other states using the setCurrentBlockState() method. Once the state is set the QTextBlock retains this value until it is set again or until the corresponding paragraph of text is deleted.
Example 2
The following example uses a block value of 1 to signify when the given text is part of a comment.
The code begins by setting the current block state to 0. If the previous block of text ended within a comment the text is highlighted from the beginning of the current block. Otherwise, a search is done to find out if a comment starts in the current block.
If the current block ends inside a comment, the block state is changed from 0 to 1 by calling setCurrentBlockState() so the following block can be highlighted.
There are two other methods which can be called to query the current formatting and user data. The are format() and currentBlockUserData().
The setCurrentBlockUserData() method can be used to associate user data with the current text block. QTextBlockUserData can be used to store custom settings.
void MySyntax::highlightBlock(
const QString &text) {
int startIndex = 0;
match = startExpression.match(text);
}
match = endExpression.match(text, match.
capturedEnd());
int commentLength;
} else {
commentLength = text.size() - startIndex;
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
match = startExpression.match(text, match.
capturedEnd());
}
}
- See also
- QTextEdit