begin process at 2012 02 13 13:55:59
  Trouver un code source :
 
dans
 
Accueil > Forum > 

C++ & C++ .NET

 > 

Divers

 > 

Général

 > 

[QT] colorier les commentaires multilignes avec QSyntaxHighlighting


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

[QT] colorier les commentaires multilignes avec QSyntaxHighlighting

mercredi 20 mai 2009 à 23:43:34 | [QT] colorier les commentaires multilignes avec QSyntaxHighlighting

christophedlr

Membre Club
Bonsoir,

J'ai regardé comment utiliser la classe QSyntaxHighlighter de QT afin de colorier du texte.
Je gère sans aucune difficulté les commentaires sur une ligne et les mots-clés.

Je souhaite utiliser les Regexp (comme pour les commantaires sur une ligne et les mots-clés) pour les commentaires multilignes.

Le problème étant que cela fonctionne que si le commentaire est sur une ligne.

Voici le code de la classe implémentant la coloration :

highlight.cpp :
#include <QtGui>
#include <iostream>
#include "highlight.h"
using namespace std;

Highlighter::Highlighter(QTextDocument *parent)
     : QSyntaxHighlighter(parent)
 {
     HighlightingRule rule;

     keywordFormat.setForeground(QColor(0, 153, 255));
     keywordFormat.setFontWeight(QFont::Bold);
     QStringList keywordPatterns;
     keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
                     << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
                     << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
                     << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
                     << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
                     << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
                     << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
                     << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
                     << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
                     << "\\bvoid\\b" << "\\bvolatile\\b";
     foreach (const QString &pattern, keywordPatterns) {
         rule.pattern = QRegExp(pattern);
         rule.format = keywordFormat;
         highlightingRules.append(rule);
     }
     
     commentFormat.setForeground(QColor(255, 132, 0));
     rule.pattern = QRegExp("//[^\n]*");
     rule.format = commentFormat;
     highlightingRules.append(rule);
     
     multiLineCommentFormat.setForeground(QColor(204, 0, 0));
     rule.pattern = QRegExp("/\\*.*\\*/");
     rule.format = multiLineCommentFormat;
     highlightingRules.append(rule);
 }

 void Highlighter::highlightBlock(const QString &text)
 {
     foreach (const HighlightingRule &rule, highlightingRules) {
         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);
     }*/
 }

highlight.h :
 #ifndef HIGHLIGHTER_H
 #define HIGHLIGHTER_H

 #include <QSyntaxHighlighter>

 #include <QHash>
 #include <QTextCharFormat>

 class QTextDocument;

 class Highlighter : public QSyntaxHighlighter
 {
     Q_OBJECT

 public:
     Highlighter(QTextDocument *parent = 0);

 protected:
     void highlightBlock(const QString &text);

 private:
     struct HighlightingRule
     {
         QRegExp pattern;
         QTextCharFormat format;
     };
     QVector<HighlightingRule> highlightingRules;

     QRegExp commentStartExpression;
     QRegExp commentEndExpression;

     QTextCharFormat keywordFormat;
     QTextCharFormat commentFormat;
     QTextCharFormat multiLineCommentFormat;
 };

#endif

L'exemple donné par QT est la partie commenté, ils crée deux QTextCharFormat un contenant en regexp le début du type de commentaire, et le second la fin.

Alors que moi je souhaite faire avec un seul est simple regexp.

Merci d'avance pour votre aide.


Cette discussion est classée dans : text, pattern, rule, startindex, highlighter


Répondre à ce message

Sujets en rapport avec ce message

ODBC Connect dialog: lacks text if api called from dll [ par mfritschi ] hi,i am writing an abstract data access framework. it enables us amongother things to access databases without being interested in theactual underlyin [VC++] Taille du text dans une application console [ par Cybmat ] SalutVoila je voudrai savoir comment on change la taille du text ecrit dans uneapplication console avec printf() .Merci d'avance Urgent problem de lecture de fichier [ par deamon ] J'ai besoin d'aide, je voudrais faire un héditeur hexadecimal en Visual C++. Le problem est que je n'arrive pas a afficher un fichier en héxa dans un multiple definition de fonctions ! [ par bidules ] bonjour,lorsque je compile j'ai plein d'erreur me disant qu'il ya des multiple definitions dans mes fichiers objets : ds tou les fichier .h il y a au property page avec les activex OCX [ par stef2000 ] je souhaite integrer une combo box dans ma property page. Je souhaite l'iniatialiser avec une liste de valeurs possible pour une propriété via OnInit COULEUR DE TEXT (SVP) [ par malito ] Je n'arrive pas malgres mes recherches a mettre du text ou l'arriere plan en couleur... textcolor(...); ne marche qu'en CComment fait Menu.... [ par erasor ] slt voila g prit une source d'un menu et je voudrai mettre une image en background o lieu d'une couleur ,voila la zone specifie: int x=cvar.menu_x, y= J'arrive pas HELP ME [ par g0belin ] system("net send "+Edit1->Text+" "+Edit2->Text);erreur:[C++ Erreur] Unit1.cpp(36): E2034 Impossible de convertir 'AnsiString' en 'const char *' c:\windows\escritorio\mes_progs\ddraw.o(.text+0x2be):ddraw.cpp: undefined reference to `timeGetTime@0' [ par felixboxer ] une erreur intervien lor de ma compilation , si qq peut maider ...c:\windows\escritorio\mes_progs\ddraw.o(.text+0x2be):ddraw.cpp: undefined reference Win sock sous dev c++ ? ? ?? ? [ par marelo87 ] salut,voila je programme sous dev c++ et lorsque je compile maus apli reseau voila les pb que j ai:C:\WINDOWS\TEMP\ccdGsagb.o(.text+0x12a):essai.c: un


Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,154 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales