Seite 1 von 1

[gelöst] QSyntaxHighlighter bei 2 verschieben Einsätzen

Verfasst: 21. Dezember 2009 23:05
von 24dan
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?

Verfasst: 21. Dezember 2009 23:24
von upsala
Wie meinen?

Du kannst das selbe QSyntaxHighlighter-Objekt nur für jeweils ein QTextEdit-Objekt verwenden.

Verfasst: 21. Dezember 2009 23:36
von 24dan
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

Verfasst: 22. Dezember 2009 09:18
von upsala

Code: Alles auswählen

[blue]=>RegEx
[green]=>RegEx
...
Bei dieser einfachen Anforderung, kann man das auch in eine Klasse verpacken.

Verfasst: 22. Dezember 2009 15:56
von 24dan
das hab ich nicht verstanden: hier mal testweise meine zwei Klassen:

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;
 };
highlighterStatus.cpp

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);
     }
 }
highlighterEingabe.h

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;
 };
highlighterEingabe

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);
     }
 }
Aufruf im Programm

Code: Alles auswählen

  highlighterStatus = new HighlighterStatus(ui->textEditStatusfenster->document());
    highlighterEingabe = new HighlighterEingabe(ui->textEditEingabe->document());