Skip to content

PythonQt.QtGui.QSyntaxHighlighter · declarations 001

Back to PythonQt.QtGui.QSyntaxHighlighter

currentBlock

currentBlock() -> Any

X.currentBlock() -> PythonQt.QtGui.QTextBlock

Supplemental documentation: Qt 5 reference:

[protected] QTextBlock QSyntaxHighlighter::currentBlock() const

Returns the current text block.

This function was introduced in Qt 4.4.

Upstream PySide2 reference signatures (binding differences may apply):

currentBlock(self) -> PySide2.QtGui.QTextBlock

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#currentBlock

currentBlockState

currentBlockState() -> int

X.currentBlockState() -> int

Supplemental documentation: Qt 5 reference:

[protected] int QSyntaxHighlighter::currentBlockState() const

Returns the state of the current text block. If no value is set, the returned value is -1.

See also setCurrentBlockState().

Upstream PySide2 reference signatures (binding differences may apply):

currentBlockState(self) -> int

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#currentBlockState

currentBlockUserData

currentBlockUserData() -> Any

X.currentBlockUserData() -> PythonQt.QtGui.QTextBlockUserData

Supplemental documentation: Qt 5 reference:

[protected] QTextBlockUserData *QSyntaxHighlighter::currentBlockUserData() const

Returns the QTextBlockUserData object previously attached to the current text block.

See also QTextBlock::userData() and setCurrentBlockUserData().

Upstream PySide2 reference signatures (binding differences may apply):

currentBlockUserData(self) -> PySide2.QtGui.QTextBlockUserData

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#currentBlockUserData

destroyed

destroyed(*args: Any, **kwargs: Any) -> Any

Native PythonQt.QtGui.QSyntaxHighlighter.destroyed; argument details are not documented.

document

document() -> Any

X.document() -> PythonQt.QtGui.QTextDocument

Supplemental documentation: Qt 5 reference:

QTextDocument *QSyntaxHighlighter::document() const

Returns the QTextDocument on which this syntax highlighter is installed.

See also setDocument().

Upstream PySide2 reference signatures (binding differences may apply):

document(self) -> PySide2.QtGui.QTextDocument

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#document

format

format(pos: int) -> Any

X.format(pos) -> PythonQt.QtGui.QTextCharFormat

Supplemental documentation: Qt 5 reference:

[protected] QTextCharFormat QSyntaxHighlighter::format(int position) const

Returns the format at position inside the syntax highlighter's current text block.

See also setFormat().

Upstream PySide2 reference signatures (binding differences may apply):

format(self, pos: int) -> PySide2.QtGui.QTextCharFormat

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#format

highlightBlock

highlightBlock(text: str) -> None

X.highlightBlock(text)

Supplemental documentation: Qt 5 reference:

[pure virtual protected] void QSyntaxHighlighter::highlightBlock(const QString \&text)

Highlights the given text block. This function is called when necessary by the rich text engine, i.e. on text blocks which have changed.

To provide your own syntax highlighting, you must subclass QSyntaxHighlighter and reimplement highlightBlock(). In your reimplementation you should parse the block's text and call setFormat() as often as necessary to apply any font and color changes that you require. 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);
    }
}

See the Detailed Description for examples of using setCurrentBlockState(), currentBlockState() and previousBlockState() to handle syntaxes with constructs that span several text blocks

See also previousBlockState(), setFormat(), and setCurrentBlockState().

Upstream PySide2 reference signatures (binding differences may apply):

highlightBlock(self, text: str) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#highlightBlock

objectNameChanged

objectNameChanged(*args: Any, **kwargs: Any) -> Any

Native PythonQt.QtGui.QSyntaxHighlighter.objectNameChanged; argument details are not documented.

previousBlockState

previousBlockState() -> int

X.previousBlockState() -> int

Supplemental documentation: Qt 5 reference:

[protected] int QSyntaxHighlighter::previousBlockState() const

Returns the end state of the text block previous to the syntax highlighter's current block. If no value was previously set, the returned value is -1.

See also highlightBlock() and setCurrentBlockState().

Upstream PySide2 reference signatures (binding differences may apply):

previousBlockState(self) -> int

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#previousBlockState

rehighlight

rehighlight() -> None

X.rehighlight()

Supplemental documentation: Qt 5 reference:

[slot] void QSyntaxHighlighter::rehighlight()

Reapplies the highlighting to the whole document.

This function was introduced in Qt 4.2.

See also rehighlightBlock().

Upstream PySide2 reference signatures (binding differences may apply):

rehighlight(self) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#rehighlight

rehighlightBlock

rehighlightBlock(block: Any) -> None

X.rehighlightBlock(block)

Supplemental documentation: Qt 5 reference:

[slot] void QSyntaxHighlighter::rehighlightBlock(const QTextBlock \&block)

Reapplies the highlighting to the given QTextBlock block.

This function was introduced in Qt 4.6.

See also rehighlight().

Upstream PySide2 reference signatures (binding differences may apply):

rehighlightBlock(self, block: PySide2.QtGui.QTextBlock) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#rehighlightBlock

setCurrentBlockState

setCurrentBlockState(newState: int) -> None

X.setCurrentBlockState(newState)

Supplemental documentation: Qt 5 reference:

[protected] void QSyntaxHighlighter::setCurrentBlockState(int newState)

Sets the state of the current text block to newState.

See also currentBlockState() and highlightBlock().

Upstream PySide2 reference signatures (binding differences may apply):

setCurrentBlockState(self, newState: int) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#setCurrentBlockState

setCurrentBlockUserData

setCurrentBlockUserData(data: Any) -> None

X.setCurrentBlockUserData(data)

Supplemental documentation: Qt 5 reference:

[protected] void QSyntaxHighlighter::setCurrentBlockUserData(QTextBlockUserData *data)

Attaches the given data to the current text block. The ownership is passed to the underlying text document, i.e. the provided QTextBlockUserData object will be deleted if the corresponding text block gets deleted.

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 example while parsing the text, you can keep track of parenthesis characters that you encounter ('{[(' and the like), and store their relative position and the actual QChar in a simple class derived from QTextBlockUserData:

struct ParenthesisInfo
{
    QChar char;
    int position;
};

struct BlockData : public QTextBlockUserData
{
    QVector<ParenthesisInfo> parentheses;
};

During cursor navigation in the associated editor, you can ask the current QTextBlock (retrieved using the QTextCursor::block() function) if it has a user data object set and cast it to your BlockData object. Then you can check if the current cursor position matches with a previously recorded parenthesis position, and, depending on the type of parenthesis (opening or closing), find the next opening or closing parenthesis on the same level.

In this way you can do a visual parenthesis matching and highlight from the current cursor position to the matching parenthesis. That makes it easier to spot a missing parenthesis in your code and to find where a corresponding opening/closing parenthesis is when editing parenthesis intensive code.

See also currentBlockUserData() and QTextBlock::setUserData().

Upstream PySide2 reference signatures (binding differences may apply):

setCurrentBlockUserData(self, data: PySide2.QtGui.QTextBlockUserData) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#setCurrentBlockUserData

setDocument

setDocument(doc: Any) -> None

X.setDocument(doc)

Supplemental documentation: Qt 5 reference:

void QSyntaxHighlighter::setDocument(QTextDocument *doc)

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

See also document().

Upstream PySide2 reference signatures (binding differences may apply):

setDocument(self, doc: PySide2.QtGui.QTextDocument) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#setDocument

setFormat

setFormat(start: int, count: int, color: Any) -> None

X.setFormat(start, count, color)

Supplemental documentation: Qt 5 reference:

[protected] void QSyntaxHighlighter::setFormat(int start, int count, const QTextCharFormat \&format)

This function is applied to the syntax highlighter's current text block (i.e. the text that is passed to the highlightBlock() function).

The specified format is applied to the text from the start position for a length of count characters (if count is 0, nothing is done). The formatting properties set in format are merged at display time with the formatting information stored directly in the document, for example as previously set with QTextCursor's functions. Note that the document itself remains unmodified by the format set through this function.

See also format() and highlightBlock().

[protected] void QSyntaxHighlighter::setFormat(int start, int count, const QColor \&color)

This is an overloaded function.

The specified color is applied to the current text block from the start position for a length of count characters.

The other attributes of the current text block, e.g. the font and background color, are reset to default values.

See also format() and highlightBlock().

[protected] void QSyntaxHighlighter::setFormat(int start, int count, const QFont \&font)

This is an overloaded function.

The specified font is applied to the current text block from the start position for a length of count characters.

The other attributes of the current text block, e.g. the font and background color, are reset to default values.

See also format() and highlightBlock().

Upstream PySide2 reference signatures (binding differences may apply):

setFormat(self, start: int, count: int, color: PySide2.QtGui.QColor) -> None
setFormat(self, start: int, count: int, font: PySide2.QtGui.QFont) -> None
setFormat(self, start: int, count: int, format: PySide2.QtGui.QTextCharFormat) -> None

Source: https://doc.qt.io/archives/qt-5.15/qsyntaxhighlighter.html#setFormat