[gelöst] QSyntaxHighlighter - Mehrzeiliger Kommentar

Alles rund um die Programmierung mit Qt
Antworten
whit3
Beiträge: 42
Registriert: 1. Februar 2010 09:55

[gelöst] QSyntaxHighlighter - Mehrzeiliger Kommentar

Beitrag von whit3 »

Hallo ich benutze QT3 und kann momentan nicht 4 benutzen.

Der mehrzeilige Kommentar funktioniert nicht richtig.
Trotz dass */ vorkommt. Werden alle Zeichen in der Zeile rot gezeichnet.
Hier der Code:

Code: Alles auswählen

int SyntaxHighlighter::highlightParagraph(const QString & text, int endStateOfLastPara) 
{
	int index = 0;
	int count = 0;
	QStringList::iterator it = keywordList.begin();
	 
	setFormat(0, text.length(), textEdit()->font(), Qt::black);
	 
	int currentState = endStateOfLastPara;
	 
	if (endStateOfLastPara == NORMAL_STATE || endStateOfLastPara < 0) 
	{
		if (text.contains("/*")) 
		{
			setFormat(0, text.length(), commentFont, commentColor);
			currentState = COMMENT_STATE;
		} 
		else 
		{
			while (it != keywordList.end()) 
			{
				count = text.contains(*it);
				if (count) 
				{
					for (int i = 0; i < count; i++) 
					{
						index = text.find(*it);
						setFormat(index, (*it).length(), keyFont, keyColor);
						++index;
					}
				}
				++it;
			}
		}
	} 
	else if (endStateOfLastPara == COMMENT_STATE) 
	{
		setFormat(0, text.length(), commentFont, commentColor);
		if (text.contains("*/"))
		currentState = NORMAL_STATE;
	} 
	return currentState;
}
Zuletzt geändert von whit3 am 28. März 2011 10:46, insgesamt 1-mal geändert.
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

In der Doku von Qt4 ist ein Demo-Code der auch mit Qt3 funktionieren müsste.
whit3
Beiträge: 42
Registriert: 1. Februar 2010 09:55

Beitrag von whit3 »

vielen dank werde ich mal nachschauen, meld mich dann wieder.
aber ich dachte bei QT4 wurde die Klasse komplett überarbeitet....
whit3
Beiträge: 42
Registriert: 1. Februar 2010 09:55

Beitrag von whit3 »

Hier die Lösung falls es jemand auch mal benötigt :

Code: Alles auswählen

SyntaxHighlighter::SyntaxHighlighter(QTextEdit* textEdit) : QSyntaxHighlighter(textEdit) 
{
	keyFont.setBold(true);
	keyFont.setItalic(true);
	keyFont.setUnderline(true);
	keyColor = Qt::blue; 
	commentColor = Qt::red;
	commentDoxyColor = Qt::green;
	commentFont.setItalic(true);
}

SyntaxHighlighter::~SyntaxHighlighter() 
{

}

void SyntaxHighlighter::addKeyword(const QString &str) 
{
	keywordList << str;
}

int SyntaxHighlighter::highlightParagraph(const QString & text, int endStateOfLastPara) 
{
   int index = 0;
   int count = 0;
   QStringList::iterator it = keywordList.begin();
   
   setFormat(0, text.length(), textEdit()->font(), Qt::black);
   
   int currentState = endStateOfLastPara;
   
   if (endStateOfLastPara == NORMAL_STATE || endStateOfLastPara < 0)
   {
      if (text.contains("/*"))
      {
         int posstart = text.find("/*");
		 setFormat(posstart, text.length(), commentFont, commentColor);
         currentState = COMMENT_STATE;
      }
      else
      {
         while (it != keywordList.end())
         {
            count = text.contains(*it);
            if (count)
            {
               for (int i = 0; i < count; i++)
               {
                  index = text.find(*it);
                  setFormat(index, (*it).length(), keyFont, keyColor);
                  ++index;
               }
            }
            ++it;
         }
      }
   }
   else if (endStateOfLastPara == COMMENT_STATE)
   {
      
	  const char*ds = text;
	  setFormat(0, text.length(), commentFont, commentColor);
      if (text.contains("*/"))
	  {
		int posend = text.findRev("*/");
		setFormat(posend+2, text.length(), textEdit()->font(), Qt::black);
		currentState = NORMAL_STATE;
	  }
   }
   return currentState; 
}
Antworten