Detail
- QT-VERSION: 6.9.0 MSVC
Die hervorzuhebenden Wörter werden abgeglichen.
Highlight-Code in der Klasse MySyntaxHighlighter:
Dieser Code ähnelt im Wesentlichen der offiziellen Qt-Demo.< /p>
Code: Select all
void highlightBlock( const QString& text ) override
{
for ( const HighlightingRule& rule : highlightingRules )
{
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch( text );
while ( matchIterator.hasNext( ) )
{
QRegularExpressionMatch match = matchIterator.next( );
setFormat( match.capturedStart( ), match.capturedLength( ), rule.format );
}
}
// Support multi-line comments
highlightMultilineComments( text );
}
Code: Select all
editor = new CodeEditor( currentTheme, editorContiner );
MySyntaxHighlighter* highlighter = new MySyntaxHighlighter( currentTheme, editor->document( ) );
Code: Select all
explicit CodeEditor( theme* cur_theme, QWidget* parent = nullptr )
: QPlainTextEdit( parent )
{
backgroundColor = cur_theme->backgroundColor;
lineNumberAreaTextColor = cur_theme->lineNumberAreaTextColor;
lineNumberAreaColor = cur_theme->lineNumberAreaColor;
currentColor = cur_theme->currentColor;
fontSize = cur_theme->fontSize;
tabSize = cur_theme->tabSize;
lineNumber = new LineNumberArea( parent, lineNumberAreaColor, lineNumberAreaTextColor, this );
connect( this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth );
connect( this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea );
connect( this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine );
updateLineNumberAreaWidth( 0 );
setFont( QFont( "Courier", fontSize ) );
setTabStopDistance( tabSize * fontMetrics( ).horizontalAdvance( ' ' ) );
connect( this, &QPlainTextEdit::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine );
QPalette p = this->palette( );
p.setColor( QPalette::Active, QPalette::Base, backgroundColor );
p.setColor( QPalette::Inactive, QPalette::Base, backgroundColor );
this->setPalette( p );
highlightCurrentLine( );
}
Ich habe versucht, den Zeilenumbruchmodus des Eingabefelds umzuschalten, aber es hatte keine Wirkung.