reuabreliz
27th December 2009, 18:40
First here is my code:
//My_Highlightingrule.h
class My_Highlightingrule
{
public:
My_Highlightingrule(QRegExp* ,QTextCharFormat* );
QRegExp* getPattern();
QTextCharFormat* getFormat();
private:
QRegExp* pattern;
QTextCharFormat* format;
};
//My_Highlightingrule_KeyWords.h
class My_Highlightingrule_KeyWords : public QList<My_Highlightingrule*>
{
public:
My_Highlightingrule_KeyWords();
};
//My_Highlightingrule_KeyWords.cpp
#include "my_highlightingrule_keyWords.h"
My_Highlightingrule_KeyWords::My_Highlightingrule_ KeyWords()
{
const int MAX = 51;
std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
"switch", "assert", "default", "goto",
"package", "synchronized", "boolean",
"do", "if", "private", "this", "break",
"double", "implements", "protected",
"throw", "byte", "else", "import",
"public", "throws", "case", "enum",
"instanceof", "return", "transient",
"catch", "extends", "int", "short",
"try", "char", "final", "interface",
"static", "void", "class", "finally",
"long", "strictfp", "volatile", "const",
"float", "native", "super", "while"};
QTextCharFormat format;
QBrush brush(QColor(255,0,0,127));
format.setForeground(brush);
QRegExp *pattern;
for(int i = 0; i < MAX; i++)
{
pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
My_Highlightingrule *rule = new My_Highlightingrule(pattern,&format);
insert(i,rule);
}
std::cout << at(49)->getPattern()->pattern()->toStdString() << std::endl; //NO SEGMENTATION FAULT
delete(pattern);
}
//My highlight function...does not highlight now
void My_CppSyntaxHighlighter::highlight(const QString & text)
{
QList<My_Highlightingrule*> rules;
My_Highlightingrule_KeyWords keyWords = y_Highlightingrule_KeyWords();
std::cout << keyWords.at(49)->getPattern()->pattern()->toStdString() << std::endl; //SEGMENTATION FAULT
rules += keyWords; //KeyWords, and so on
}
Ahhm, as you see I have not inserted the #includes and so on.
What I want:
I want a Class My_CppHIghlightingrules which derive from My_SyntaxHighlighting (Is only a Abstract class with a member highlight(const QString&). The function highlight(const QString& text) in My_CppHighlightingrules should add some Highlightingrules (e.g.: My_Highlighting_KeyWords) and then go with a loop through all rules and return the new text.
Problem:
My Problem is this. When I create an object of My_Highlightingrule_Keywords in highlight(const QString&) and I want the last Keyword "while" (see My_Highlightingrule_Keywords() ) then it gives me an Segmentation fault. But when I print the last Keyword "while" in My_Highlightingrule_KeyWords (see My_Highlighting_Keywords() ) then it gives me no Segmentation fault. WHY????? I don't understand it. Please help me.
//My_Highlightingrule.h
class My_Highlightingrule
{
public:
My_Highlightingrule(QRegExp* ,QTextCharFormat* );
QRegExp* getPattern();
QTextCharFormat* getFormat();
private:
QRegExp* pattern;
QTextCharFormat* format;
};
//My_Highlightingrule_KeyWords.h
class My_Highlightingrule_KeyWords : public QList<My_Highlightingrule*>
{
public:
My_Highlightingrule_KeyWords();
};
//My_Highlightingrule_KeyWords.cpp
#include "my_highlightingrule_keyWords.h"
My_Highlightingrule_KeyWords::My_Highlightingrule_ KeyWords()
{
const int MAX = 51;
std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
"switch", "assert", "default", "goto",
"package", "synchronized", "boolean",
"do", "if", "private", "this", "break",
"double", "implements", "protected",
"throw", "byte", "else", "import",
"public", "throws", "case", "enum",
"instanceof", "return", "transient",
"catch", "extends", "int", "short",
"try", "char", "final", "interface",
"static", "void", "class", "finally",
"long", "strictfp", "volatile", "const",
"float", "native", "super", "while"};
QTextCharFormat format;
QBrush brush(QColor(255,0,0,127));
format.setForeground(brush);
QRegExp *pattern;
for(int i = 0; i < MAX; i++)
{
pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
My_Highlightingrule *rule = new My_Highlightingrule(pattern,&format);
insert(i,rule);
}
std::cout << at(49)->getPattern()->pattern()->toStdString() << std::endl; //NO SEGMENTATION FAULT
delete(pattern);
}
//My highlight function...does not highlight now
void My_CppSyntaxHighlighter::highlight(const QString & text)
{
QList<My_Highlightingrule*> rules;
My_Highlightingrule_KeyWords keyWords = y_Highlightingrule_KeyWords();
std::cout << keyWords.at(49)->getPattern()->pattern()->toStdString() << std::endl; //SEGMENTATION FAULT
rules += keyWords; //KeyWords, and so on
}
Ahhm, as you see I have not inserted the #includes and so on.
What I want:
I want a Class My_CppHIghlightingrules which derive from My_SyntaxHighlighting (Is only a Abstract class with a member highlight(const QString&). The function highlight(const QString& text) in My_CppHighlightingrules should add some Highlightingrules (e.g.: My_Highlighting_KeyWords) and then go with a loop through all rules and return the new text.
Problem:
My Problem is this. When I create an object of My_Highlightingrule_Keywords in highlight(const QString&) and I want the last Keyword "while" (see My_Highlightingrule_Keywords() ) then it gives me an Segmentation fault. But when I print the last Keyword "while" in My_Highlightingrule_KeyWords (see My_Highlighting_Keywords() ) then it gives me no Segmentation fault. WHY????? I don't understand it. Please help me.