Change of syntax highlighter
-
Hi, I again have a small problem with my aplication where I have menu to choise a syntax higlighter, and I don't know how to do this. For now I tried on two ways, first was looking like this:
@
class first : public QSyntaxHighlighter{
//first highlighter
};
class second : public QSyntaxHighlighter{
//second highlighter
};
//in MainWindow i have index for QSyntaxHiglighter
*QSyntaxHighlighter *highlighter;
//and I have this slots
void MianWindow::firstHighlight(){
highlighter = NULL;
highlighter = new first(this);
editor->update();
}
void MainWindow::secondHighlight(){
highlighter = NULL;
highlighter = new second(this);
editor->update();
}
@but it dosen't works, application is crashing when I want to change from first higlight to second highlight. And funny think is that when I want to change first higlight to first it works :)
My second tried lokking like this:
@
class Highlight : public QSyntaxHiglighter{
//definicion of class
public:
void set_active(int i = 0);
int get_active();
private:
int active;
};
//in constructor I have switch segment
Highlight::Highlight(QWidget *parent) : QSyntaxHiglighter(parent){
switch (active){
case 1: // first highlight
break;
case 2: // second highlight
break;
default: // without highlight
}//end of switch
}
@
And in this apllication is crashing on the begining... None of this ways have compile errors.
Can you help me with this? I am fighting with this from two days... -
I don't know a quick solution for your changing problem, but you create memory leaks.
Also, you should pass a QTextEdit or QTextDocument to the "QSyntaxHighlighter constructor":http://doc.qt.nokia.com/4.7/qsyntaxhighlighter.html#QSyntaxHighlighter-2
Maybe that already solves your problem.
@
delete highlighter;
highligher = new xxx(editor->document());
@And don't forget to initialize the highlighter variable to 0 in the constructor, otherwise the delete will fail.
-
thanks for catch this memory leak :) but this don't solves my problem, and i don't know why my application started a strange behavior for example when I want to open new text file in new tab apllication is crashing... :( maybe problem is in somewhere else.
-
Sorry, with that little code we cannot comment on the problem. You clearly have some typos in it (line 8, * QSyntaxHighlighter; line 10 Mianwindow), so it seems not to be the actual code. To investigate further, a small, complete, compilable, runnable example would be helpful.
-
Oh, sorry, I don't want to paste a bid piece of code so I only do a outline of situation, but it is seems much complicated than I thought. My code for this moment:
Highlight.h
@#include <QSyntaxHighlighter>
#include <QHash>
#include <QTextCharFormat>class Highlight : public QSyntaxHighlighter
{
Q_OBJECTpublic:
Highlight(QTextDocument *parent);
protected:
void highlightBlock(const QString &text);private:
struct HighlitingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlitingRule> HiglitingRules;QRegExp commentStartExpresion; QRegExp commentEndExpresion; QTextCharFormat keywordFormat; QTextCharFormat functionFormat; QTextCharFormat variablesFormat; QTextCharFormat numberFormat; QTextCharFormat singleLineCommentFormat; QTextCharFormat multiLineCommentFormat; QTextCharFormat textFormat; QTextCharFormat text2Format;
};
class JSHighlight : public QSyntaxHighlighter
{
Q_OBJECT
public:
JSHighlight(QTextDocument *parent);
protected:
void highlightBlock(const QString &text);
private:
struct HighlitingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlitingRule> HiglitingRules;QRegExp commentStartExpresion; QRegExp commentEndExpresion; QTextCharFormat keywordFormat; QTextCharFormat functionFormat; QTextCharFormat variablesFormat; QTextCharFormat numberFormat; QTextCharFormat singleLineCommentFormat; QTextCharFormat multiLineCommentFormat;
};@
Highlight.cpp have more than 150 lines so I paste only one class, because they are very similar:
@JSHighlight::JSHighlight(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
HighlitingRule rule;
keywordFormat.setForeground(Qt::darkGreen);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\bclass\b" << "\bif\b" << "\bswitch\b" << "\bcase\b" << "\bbreak\b"
<< "\bpublic\b" << "\bprivate\b" <<"\bthis\b" <<"\bfunction\b"
<<"\bfor\b" <<"\bwhile\b" <<"\bdo\b" <<"\bforeach\b" <<"\binclude\b"
<<"\bexit\b"
<< "\belse\b" << "\btrue\b" << "\bfalse\b" <<"\bextands\b" <<"\bnew\b" <<"\barray\b"
<< "\bprotected\b" << "\bnull\b" <<"b\is_defined\b" <<"b\define\b" <<"b\abstract\b";
foreach(const QString &pattern, keywordPatterns){
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
HiglitingRules.append(rule);
}singleLineCommentFormat.setForeground(Qt::red); rule.pattern = QRegExp("//[^n]*"); rule.format = singleLineCommentFormat; HiglitingRules.append(rule); multiLineCommentFormat.setForeground(Qt::red);
}
void JSHighlight::highlightBlock(const QString &text){
foreach(const HighlitingRule &rule, HiglitingRules){
QRegExp expresion(rule.pattern);
int index = expresion.indexIn(text);
while(index >= 0){
int lenght = expresion.matchedLength();
setFormat(index,lenght,rule.format);
index = expresion.indexIn(text,index + lenght);
}
}
setCurrentBlockState(0);int startIndex = 0; if (previousBlockState() != 1) startIndex = commentStartExpresion.indexIn(text); while (startIndex >= 0) { int endIndex = commentEndExpresion.indexIn(text, startIndex); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + commentEndExpresion.matchedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = commentStartExpresion.indexIn(text, startIndex + commentLength); }
}@
MainWindow.h is to a big code, so I do a outline:
@
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void createEdito(const QString &file = ""); //creating editor in TabWidget
private:
Editor *codeedit;
TabDialog *settings;
NewFile *newfile;
QSyntaxHighlighter *highlight;
QTabWidget *tab;
void setupFileAction(); //menu File
void setupEditAction(); //menu Edit
void setupTextAction(); //menu Text
private slots:
bool load(const QString &f);
//Slots to file Action
void fileNew();
void fileOpen();
void fileSave();
//Slots to syntax Action
void php();
void js();
void html();
void sql();
void about();
void setting();
void closeTab(int a);
};
class Editor : public QPlainTextEdit{
Q_OBJECT
public:
Editor(QWidget *parent = 0);
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
protected:
void resizeEvent(QResizeEvent *event);
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &, int);
private:
QWidget *lineNumberArea;
};
class LineNumberArea : public QWidget
{
public:
LineNumberArea(Editor *editor) : QWidget(editor) {
codeEditor = editor;
}
QSize sizeHint() const {
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) {
codeEditor->lineNumberAreaPaintEvent(event);
}
private:
Editor *codeEditor;
};
@ -
My Mainwindow.cpp have 462 line, so I copy only a importent thing:
@
MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent){
tab = new QTabWidget();
tab->setMovable(true);
tab->setTabsClosable(true);
codeedit = new Editor(this);
codeedit->setTabStopWidth(30);
//highlight = new Highlight(codeedit->document());
highlight = 0;
tab->addTab(codeedit,"Hello");
codeedit->setPlainText("Welcome in WebEdit :) Have fun :)");
setCentralWidget(tab);
}
void MainWindow::createEdito(const QString &file){
codeedit = new Editor(this); //create a new editor
codeedit->setTabStopWidth(30);
codeedit->setPlainText(file);
codeedit->setWordWrapMode(QTextOption::WordWrap);
//highlight = new Highlight(codeedit->document());
highlight = 0;
tab->addTab(codeedit,"tab");
connect(tab,SIGNAL(tabCloseRequested(int)),this,SLOT(closeTab(int)));
}
void MainWindow::createEditor(const QString &filename){
codeedit = new Editor(this);
codeedit->setTabStopWidth(30);
//highlight = new Highlight(codeedit->document());
highlight = 0;
tab->addTab(codeedit,filename);
connect(tab,SIGNAL(tabCloseRequested(int)),this,SLOT(closeTab(int)));
}
void MainWindow::fileOpen(){
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
QString(), tr("HTML-Files (.htm .html);;PHP-Files(.php);;JavaScript(.js);;SQL-Files(.sql);;All Files (*)"));
if (!fn.isEmpty())
load(fn);
}
//coloring php's scripts
void MainWindow::php(){
delete highlight;
highlight = new Highlight(codeedit->document());
codeedit->update();
}
//coloring js's scripts
void MainWindow::js(){
delete highlight;
highlight = new JSHighlight(codeedit->document());
codeedit->update();
}
Editor::Editor(QWidget *parent) : QPlainTextEdit(parent)
{
lineNumberArea = new LineNumberArea(this);connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); updateLineNumberAreaWidth(0); highlightCurrentLine();
}
@
for any help I will be much greatful :)Edit: I am sorry for double post, but limit of characters don't let me to send only one message :(
-
What's so hard to understand about that
small
complete
compilable
running
example thingy? Why does nobody provide what he's asked for? Why should we bother and waste our time to pick the small pieces and put it together to a sample project? Do you have any reason why?
Please, break down your problem to a simple example program - outside your actual program that reproduces the error. Leave out everything that is not necessary to demonstrate the error. Check if every of the above points is met. Check twice. And then, only then, come back with you have. We'll be happy to look at that.