[][src]Struct qt_gui::QSyntaxHighlighter

#[repr(C)]
pub struct QSyntaxHighlighter { /* fields omitted */ }

The QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting or user data.

C++ class: QSyntaxHighlighter.

C++ documentation:

The QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting or user data.

The QSyntaxHighlighter class is a base class for implementing QTextDocument syntax highlighters. A syntax highligher automatically highlights parts of the text in a QTextDocument. Syntax highlighters are often used when the user is entering text in a specific format (for example source code) and help the user to read the text and identify syntax errors.

To provide your own syntax highlighting, you must subclass QSyntaxHighlighter and reimplement highlightBlock().

When you create an instance of your QSyntaxHighlighter subclass, pass it the QTextDocument that you want the syntax highlighting to be applied to. For example:

QTextEdit editor = new QTextEdit; MyHighlighter highlighter = new MyHighlighter(editor->document());

After this your highlightBlock() function will be called automatically whenever necessary. Use your highlightBlock() function to apply formatting (e.g. setting the font and color) to the text that is passed to it. QSyntaxHighlighter provides the setFormat() function which applies a given QTextCharFormat on the current text block. For example:

void MyHighlighter::highlightBlock(const QString &text) { QTextCharFormat myClassFormat; myClassFormat.setFontWeight(QFont::Bold); myClassFormat.setForeground(Qt::darkMagenta);

QRegularExpression expression("\bMy[A-Za-z]+\b"); QRegularExpressionMatchIterator i = expression.globalMatch(text); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); setFormat(match.capturedStart(), match.capturedLength(), myClassFormat); } }

Some syntaxes can have constructs that span several text blocks. For example, a C++ syntax highlighter should be able to cope with /*...*/ multiline comments. To deal with these cases it is necessary to know the end state of the previous text block (e.g. "in comment").

Inside your highlightBlock() implementation you can query the end state of the previous text block using the previousBlockState() function. After parsing the block you can save the last state using setCurrentBlockState().

The currentBlockState() and previousBlockState() functions return an int value. If no state is set, the returned value is -1. You can designate any other value to identify any given state using the setCurrentBlockState() function. Once the state is set the QTextBlock keeps that value until it is set set again or until the corresponding paragraph of text is deleted.

For example, if you're writing a simple C++ syntax highlighter, you might designate 1 to signify "in comment":

QTextCharFormat multiLineCommentFormat; multiLineCommentFormat.setForeground(Qt::red);

QRegularExpression startExpression("/\"); QRegularExpression endExpression("\/");

setCurrentBlockState(0);

int startIndex = 0; if (previousBlockState() != 1) startIndex = text.indexOf(startExpression);

while (startIndex >= 0) { QRegularExpressionMatch endMatch; int endIndex = text.indexOf(endExpression, startIndex, &endMatch); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + endMatch.capturedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = text.indexOf(startExpression, startIndex + commentLength); }

In the example above, we first set the current block state to 0. Then, if the previous block ended within a comment, we highlight from the beginning of the current block (startIndex = 0). Otherwise, we search for the given start expression. If the specified end expression cannot be found in the text block, we change the current block state by calling setCurrentBlockState(), and make sure that the rest of the block is highlighted.

In addition you can query the current formatting and user data using the format() and currentBlockUserData() functions respectively. You can also attach user data to the current text block using the setCurrentBlockUserData() function. QTextBlockUserData can be used to store custom settings. In the case of syntax highlighting, it is in particular interesting as cache storage for information that you may figure out while parsing the paragraph's text. For an example, see the setCurrentBlockUserData() documentation.

Methods

impl QSyntaxHighlighter[src]

pub fn slot_rehighlight(&self) -> Receiver<()>[src]

Reapplies the highlighting to the whole document.

Returns a built-in Qt slot QSyntaxHighlighter::rehighlight that can be passed to qt_core::Signal::connect.

C++ documentation:

Reapplies the highlighting to the whole document.

This function was introduced in Qt 4.2.

See also rehighlightBlock().

pub fn slot_rehighlight_block(&self) -> Receiver<(*const QTextBlock,)>[src]

Reapplies the highlighting to the given QTextBlock block.

Returns a built-in Qt slot QSyntaxHighlighter::rehighlightBlock that can be passed to qt_core::Signal::connect.

C++ documentation:

Reapplies the highlighting to the given QTextBlock block.

This function was introduced in Qt 4.6.

See also rehighlight().

pub unsafe fn document(&self) -> MutPtr<QTextDocument>[src]

Returns the QTextDocument on which this syntax highlighter is installed.

Calls C++ function: QTextDocument* QSyntaxHighlighter::document() const.

C++ documentation:

Returns the QTextDocument on which this syntax highlighter is installed.

See also setDocument().

pub unsafe fn meta_object(&self) -> Ptr<QMetaObject>[src]

Calls C++ function: virtual const QMetaObject* QSyntaxHighlighter::metaObject() const.

pub unsafe fn qt_metacall(
    &mut self,
    arg1: Call,
    arg2: c_int,
    arg3: impl CastInto<MutPtr<*mut c_void>>
) -> c_int
[src]

Calls C++ function: virtual int QSyntaxHighlighter::qt_metacall(QMetaObject::Call arg1, int arg2, void** arg3).

pub unsafe fn qt_metacast(
    &mut self,
    arg1: impl CastInto<Ptr<c_char>>
) -> MutPtr<c_void>
[src]

Calls C++ function: virtual void* QSyntaxHighlighter::qt_metacast(const char* arg1).

pub unsafe fn rehighlight(&mut self)[src]

Reapplies the highlighting to the whole document.

Calls C++ function: [slot] void QSyntaxHighlighter::rehighlight().

C++ documentation:

Reapplies the highlighting to the whole document.

This function was introduced in Qt 4.2.

See also rehighlightBlock().

pub unsafe fn rehighlight_block(
    &mut self,
    block: impl CastInto<Ref<QTextBlock>>
)
[src]

Reapplies the highlighting to the given QTextBlock block.

Calls C++ function: [slot] void QSyntaxHighlighter::rehighlightBlock(const QTextBlock& block).

C++ documentation:

Reapplies the highlighting to the given QTextBlock block.

This function was introduced in Qt 4.6.

See also rehighlight().

pub unsafe fn set_document(&mut self, doc: impl CastInto<MutPtr<QTextDocument>>)[src]

Installs the syntax highlighter on the given QTextDocument doc. A QSyntaxHighlighter can only be used with one document at a time.

Calls C++ function: void QSyntaxHighlighter::setDocument(QTextDocument* doc).

C++ documentation:

Installs the syntax highlighter on the given QTextDocument doc. A QSyntaxHighlighter can only be used with one document at a time.

See also document().

pub unsafe fn static_meta_object() -> Ref<QMetaObject>[src]

Returns a reference to the staticMetaObject field.

pub unsafe fn tr(
    s: impl CastInto<Ptr<c_char>>,
    c: impl CastInto<Ptr<c_char>>,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QSyntaxHighlighter::tr(const char* s, const char* c, int n).

pub unsafe fn tr_utf8(
    s: impl CastInto<Ptr<c_char>>,
    c: impl CastInto<Ptr<c_char>>,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QSyntaxHighlighter::trUtf8(const char* s, const char* c, int n).

Trait Implementations

impl CppDeletable for QSyntaxHighlighter[src]

unsafe fn delete(&mut self)[src]

Destructor. Uninstalls this syntax highlighter from the text document.

Calls C++ function: virtual [destructor] void QSyntaxHighlighter::~QSyntaxHighlighter().

C++ documentation:

Destructor. Uninstalls this syntax highlighter from the text document.

impl Deref for QSyntaxHighlighter[src]

type Target = QObject

The resulting type after dereferencing.

fn deref(&self) -> &QObject[src]

Calls C++ function: QObject* static_cast<QObject*>(QSyntaxHighlighter* ptr).

impl DerefMut for QSyntaxHighlighter[src]

fn deref_mut(&mut self) -> &mut QObject[src]

Calls C++ function: QObject* static_cast<QObject*>(QSyntaxHighlighter* ptr).

impl DynamicCast<QSyntaxHighlighter> for QObject[src]

unsafe fn dynamic_cast(ptr: Ptr<QObject>) -> Ptr<QSyntaxHighlighter>[src]

Calls C++ function: QSyntaxHighlighter* dynamic_cast<QSyntaxHighlighter*>(QObject* ptr).

unsafe fn dynamic_cast_mut(ptr: MutPtr<QObject>) -> MutPtr<QSyntaxHighlighter>[src]

Calls C++ function: QSyntaxHighlighter* dynamic_cast<QSyntaxHighlighter*>(QObject* ptr).

impl StaticDowncast<QSyntaxHighlighter> for QObject[src]

unsafe fn static_downcast(ptr: Ptr<QObject>) -> Ptr<QSyntaxHighlighter>[src]

Calls C++ function: QSyntaxHighlighter* static_cast<QSyntaxHighlighter*>(QObject* ptr).

unsafe fn static_downcast_mut(
    ptr: MutPtr<QObject>
) -> MutPtr<QSyntaxHighlighter>
[src]

Calls C++ function: QSyntaxHighlighter* static_cast<QSyntaxHighlighter*>(QObject* ptr).

impl StaticUpcast<QObject> for QSyntaxHighlighter[src]

unsafe fn static_upcast(ptr: Ptr<QSyntaxHighlighter>) -> Ptr<QObject>[src]

Calls C++ function: QObject* static_cast<QObject*>(QSyntaxHighlighter* ptr).

unsafe fn static_upcast_mut(ptr: MutPtr<QSyntaxHighlighter>) -> MutPtr<QObject>[src]

Calls C++ function: QObject* static_cast<QObject*>(QSyntaxHighlighter* ptr).

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.