Hi, habe mich mit der QSyntaxHighlighter Klasse vertraut gemacht und die bei mir für ein TextEdit Feld im Einsatz. Funktioniert super.
Problem: ich möchte jetzt ein zweites TextEdit Feld mit dem QSyntaxHighlighter nutzen, jedoch kollidieren hierbei meine "rules" miteinander.
Sollte ich jetzt jeweils eine komplette zweite Klasse anlegen?
Irgend eine Idee?
[gelöst] QSyntaxHighlighter bei 2 verschieben Einsätzen
[gelöst] QSyntaxHighlighter bei 2 verschieben Einsätzen
Zuletzt geändert von 24dan am 22. Dezember 2009 00:47, insgesamt 1-mal geändert.
"Der erste Trunk aus dem Becher der Naturwissenschaften macht atheistisch, aber auf dem Grund des Bechers wartet Gott."
(W. Heisenberg)
(W. Heisenberg)
naja also ich meine ich habe konkret folgendes vor:
1. Ich habe in der Klasse 3 rules für mein Statusfenster
a - makieren rot wenn etwas mit W beginnt und dies und das enthält (A-Za-z0-9._ () bla bla...
b - makiere blau wenn etwas mit F beginnt und dann wie oben
c - makiere grün wenn etwas mit I beginnt und dann wie oben
2. möchte ich gerne in meinem Texteingabefeld (QTextEdit) folgende rules anwenden:
a - alle Zahlen blau und das hier kolliediert dan schon mit dem Statusfenster rules denn dort wird jetzt das auch so "blau" makiert
b - alle kommas, Simikolons, grün
c - bla bla anders aber mit Schnittmengen aus Statusfester Rules
Ist das jetzt klarer?
Geht wohl nur mit ner zweiten Klasse ???
1. Klasse Statushighlight
2. Klasse Eingabehighlight
3. Klasse WasNochAlleshighlight mit je einem *.h & *.cpp file
1. Ich habe in der Klasse 3 rules für mein Statusfenster
a - makieren rot wenn etwas mit W beginnt und dies und das enthält (A-Za-z0-9._ () bla bla...
b - makiere blau wenn etwas mit F beginnt und dann wie oben
c - makiere grün wenn etwas mit I beginnt und dann wie oben
2. möchte ich gerne in meinem Texteingabefeld (QTextEdit) folgende rules anwenden:
a - alle Zahlen blau und das hier kolliediert dan schon mit dem Statusfenster rules denn dort wird jetzt das auch so "blau" makiert
b - alle kommas, Simikolons, grün
c - bla bla anders aber mit Schnittmengen aus Statusfester Rules
Ist das jetzt klarer?
Geht wohl nur mit ner zweiten Klasse ???
1. Klasse Statushighlight
2. Klasse Eingabehighlight
3. Klasse WasNochAlleshighlight mit je einem *.h & *.cpp file
"Der erste Trunk aus dem Becher der Naturwissenschaften macht atheistisch, aber auf dem Grund des Bechers wartet Gott."
(W. Heisenberg)
(W. Heisenberg)
Code: Alles auswählen
[blue]=>RegEx
[green]=>RegEx
...
das hab ich nicht verstanden: hier mal testweise meine zwei Klassen:
highlighterStatus.h
highlighterStatus.cpp
highlighterEingabe.h
highlighterEingabe
Aufruf im Programm
highlighterStatus.h
Code: Alles auswählen
class QTextDocument;
class HighlighterStatus : public QSyntaxHighlighter
{
Q_OBJECT
public:
HighlighterStatus(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightRuleStatus
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightRuleStatus> highlightRulesStatus;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat statusInfoFormat;
QTextCharFormat statusWarnungFormat;
QTextCharFormat statusFehlerFormat;
};
Code: Alles auswählen
#include <QtGui>
#include "highlighterStatus.h"
HighlighterStatus::HighlighterStatus(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightRuleStatus rule;
// Statusfenster
statusInfoFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\bI[A-Za-z0-9.:_ ()öäü!\t]+\\b");
rule.format = statusInfoFormat;
highlightRulesStatus.append(rule);
statusWarnungFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("\\bW[A-Za-z0-9.:_ ()öüä!\t]+\\b");
rule.format = statusWarnungFormat;
highlightRulesStatus.append(rule);
statusFehlerFormat.setForeground(Qt::red);
rule.pattern = QRegExp("\\bF[A-Za-z0-9.:_ ()öäü!\t]+\\b");
rule.format = statusFehlerFormat;
highlightRulesStatus.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
}
void HighlighterStatus::highlightBlock(const QString &text)
{
foreach (const HighlightRuleStatus &rule, highlightRulesStatus) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
Code: Alles auswählen
class QTextDocument;
class HighlighterEingabe : public QSyntaxHighlighter
{
Q_OBJECT
public:
HighlighterEingabe(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightRuleEingabe
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightRuleEingabe> highlightRulesEingabe;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat eingabeZahlFormat;
QTextCharFormat statusInfoFormat;
QTextCharFormat statusWarnungFormat;
QTextCharFormat statusFehlerFormat;
};
Code: Alles auswählen
#include <QtGui>
#include "highlighterEingabe.h"
HighlighterEingabe::HighlighterEingabe(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightRuleEingabe rule;
singleLineCommentFormat.setBackground(Qt::darkMagenta);
rule.pattern = QRegExp("[ÄäÖöÜüß]");
rule.format = singleLineCommentFormat;
highlightRulesEingabe.append(rule);
multiLineCommentFormat.setForeground(Qt::red);
// Sonderzeichen
quotationFormat.setBackground(Qt::darkGreen);
rule.pattern = QRegExp("[;:<>()/&%$§]");
rule.format = quotationFormat;
highlightRulesEingabe.append(rule);
// Zahlen
eingabeZahlFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("[0-9]{1,66}");
rule.format = eingabeZahlFormat;
highlightRulesEingabe.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
}
void HighlighterEingabe::highlightBlock(const QString &text)
{
foreach (const HighlightRuleEingabe &rule, highlightRulesEingabe) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
Code: Alles auswählen
highlighterStatus = new HighlighterStatus(ui->textEditStatusfenster->document());
highlighterEingabe = new HighlighterEingabe(ui->textEditEingabe->document());
"Der erste Trunk aus dem Becher der Naturwissenschaften macht atheistisch, aber auf dem Grund des Bechers wartet Gott."
(W. Heisenberg)
(W. Heisenberg)