Unsolved Syntax HighLight problem
-
Hello i have an error on my code but i dont know how to fix it can anyone pls help:
myhighlight.cpp:
#include "myhighlighter.h" #include <QMessageBox> #include <QFile> #include <QTextStream> #include <QRegularExpression> MyHighLighter::MyHighLighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { HighlightingRule rule; QFile MyFile("data.settings"); QString darkString("20c6ff18f58e7fb7d4a2ca57f83468ac"); QString lightString("f4bb1e47da1f05015ba255e0b4e5a2b1"); QTextStream in (&MyFile); QString line; do { line = in.readLine(); if (line.contains(darkString, Qt::CaseSensitive)) { keywordFormat.setForeground(QColor("#9477cb")); QStringList keywordPatterns; keywordPatterns << "\\bsay\\b" << "\\bask\\b" << "\\bhyperlink\\b" << "\\breadout\\b" << "\\bsay-previously-read\\b" << "\\b__root__\\b" << "\\b__name__\\b" << "\\b__package__\\b" << "\\b__doc__\\b" << "\\bdelete\\b" << "\\b__keywords__\\b" << "\\bget-html\\b" << "\\bmake\\b" << "\\buse\\b" << "\\b__system_arguments__\\b" << "\\b__change_window_title__\\b" << "\\bstart\\b" << "\\bopen\\b" << "\\bwrite\\b" << "\\bclose\\b" << "\\bwrite-previously-read\\b" << "\\bread\\b" << "\\bget-response\\b" << "\\breturn\\b" << "\\b__get_keywords__\\b" << "\\bread-current\\b" << "\\bset-cursor\\b" << "\\bcompile_atom\\b" << "\\bcompile\\b" << "\\bvar\\b" << "\\b__change_window_title_WE__\\b" << "\\bnothing\\b" << "\\bdelete-directory\\b" << "\\bget\\b" << "\\bnew-window\\b" << "\\bloop\\b" << "\\badd-text\\b" << "\\badd-inputfield\\b" << "\\bconfigure\\b" << "\\badd-space\\b" << "\\berrorize\\b" << "\\blift\\b" << "\\bequation\\b" << "\\bwait\\b" << "\\bexit\\b" << "\\badd-button\\b" << "\\bforget\\b" << "\\bdestroy-window\\b" << "\\bdisable\\b" << "\\benable\\b" << "\\bchange-directory\\b"; foreach (const QString &pattern, keywordPatterns) { rule.pattern = QRegExp(pattern); rule.format = keywordFormat; highlightingRules.append(rule); } ifFormat.setForeground(QColor("#4d4dff")); QStringList ifPatterns; ifPatterns << "\\bif\\b" << "\\bdefault:" << "\\bor\\b" << "\\band\\b" << "\\bTrue\\b" << "\\bFalse\\b" << "\\bNone\\b"; foreach (const QString &pattern, ifPatterns) { rule.pattern = QRegExp(pattern); rule.format = ifFormat; highlightingRules.append(rule); } txt.setForeground(QColor("#00ffae")); rule.pattern = QRegExp("\\W[0-9]\\W"); rule.format = txt; highlightingRules.append(rule); singleLineCommentFormat.setForeground(Qt::darkGray); rule.pattern = QRegExp("##[^\n]*"); rule.format = singleLineCommentFormat; highlightingRules.append(rule); fFormat.setForeground(Qt::yellow); rule.pattern = QRegExp("\\$[^\n]*:"); rule.format = fFormat; highlightingRules.append(rule); quotationFormat.setForeground(QColor("#008f30")); rule.pattern = QRegExp("\"[^\n]*\""); rule.format = quotationFormat; highlightingRules.append(rule); quotationFormat.setForeground(QColor("#008f30")); rule.pattern = QRegExp("\'[^\n]*\'"); rule.format = quotationFormat; highlightingRules.append(rule); } else if (line.contains(lightString, Qt::CaseSensitive)){ } } while (!line.isNull()); void MyHighLighter::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); } }
it gave error:
myhighlighter.cpp:84:57: error: function definition is not allowed here
pls help,thx alot
im reduce confusion,i made a theme settings,it check the file data.settings have what text inside and use theme if the file have the specified text it will set what theme.
thats why i need use QFile on syntax highlight,bcuz when change theme,the syntax highlight will make the text blur and not able to look at -
@ELEMENTICY
I'm going to say this, though not the answer you wanted, but it's meant to help you. You really, really should be able to see this one yourself. I don't know the line numbers but can see just by glancing, but you have the line number from the compiler error message to actually go to. Did you do that?This is not a question to post on a Qt forum for others to solve for you.
-
@JonB oh i saw it,sorry
This is my current code with no error:#include "myhighlighter.h" #include <QMessageBox> #include <QFile> #include <QTextStream> #include <QRegularExpression> MyHighLighter::MyHighLighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { HighlightingRule rule; QFile MyFile("data.settings"); QString darkString("20c6ff18f58e7fb7d4a2ca57f83468ac"); QString lightString("f4bb1e47da1f05015ba255e0b4e5a2b1"); QTextStream in (&MyFile); QString line; do { line = in.readLine(); if (line.contains(darkString, Qt::CaseSensitive)) { keywordFormat.setForeground(QColor("#9477cb")); QStringList keywordPatterns; keywordPatterns << "\\bsay\\b" << "\\bask\\b" << "\\bhyperlink\\b" << "\\breadout\\b" << "\\bsay-previously-read\\b" << "\\b__root__\\b" << "\\b__name__\\b" << "\\b__package__\\b" << "\\b__doc__\\b" << "\\bdelete\\b" << "\\b__keywords__\\b" << "\\bget-html\\b" << "\\bmake\\b" << "\\buse\\b" << "\\b__system_arguments__\\b" << "\\b__change_window_title__\\b" << "\\bstart\\b" << "\\bopen\\b" << "\\bwrite\\b" << "\\bclose\\b" << "\\bwrite-previously-read\\b" << "\\bread\\b" << "\\bget-response\\b" << "\\breturn\\b" << "\\b__get_keywords__\\b" << "\\bread-current\\b" << "\\bset-cursor\\b" << "\\bcompile_atom\\b" << "\\bcompile\\b" << "\\bvar\\b" << "\\b__change_window_title_WE__\\b" << "\\bnothing\\b" << "\\bdelete-directory\\b" << "\\bget\\b" << "\\bnew-window\\b" << "\\bloop\\b" << "\\badd-text\\b" << "\\badd-inputfield\\b" << "\\bconfigure\\b" << "\\badd-space\\b" << "\\berrorize\\b" << "\\blift\\b" << "\\bequation\\b" << "\\bwait\\b" << "\\bexit\\b" << "\\badd-button\\b" << "\\bforget\\b" << "\\bdestroy-window\\b" << "\\bdisable\\b" << "\\benable\\b" << "\\bchange-directory\\b"; foreach (const QString &pattern, keywordPatterns) { rule.pattern = QRegExp(pattern); rule.format = keywordFormat; highlightingRules.append(rule); } ifFormat.setForeground(QColor("#4d4dff")); QStringList ifPatterns; ifPatterns << "\\bif\\b" << "\\bdefault:" << "\\bor\\b" << "\\band\\b" << "\\bTrue\\b" << "\\bFalse\\b" << "\\bNone\\b"; foreach (const QString &pattern, ifPatterns) { rule.pattern = QRegExp(pattern); rule.format = ifFormat; highlightingRules.append(rule); } txt.setForeground(QColor("#00ffae")); rule.pattern = QRegExp("\\W[0-9]\\W"); rule.format = txt; highlightingRules.append(rule); singleLineCommentFormat.setForeground(Qt::darkGray); rule.pattern = QRegExp("##[^\n]*"); rule.format = singleLineCommentFormat; highlightingRules.append(rule); fFormat.setForeground(Qt::yellow); rule.pattern = QRegExp("\\$[^\n]*:"); rule.format = fFormat; highlightingRules.append(rule); quotationFormat.setForeground(QColor("#008f30")); rule.pattern = QRegExp("\"[^\n]*\""); rule.format = quotationFormat; highlightingRules.append(rule); quotationFormat.setForeground(QColor("#008f30")); rule.pattern = QRegExp("\'[^\n]*\'"); rule.format = quotationFormat; highlightingRules.append(rule); } else if (line.contains(lightString, Qt::CaseSensitive)){ } } while (!line.isNull()); } void MyHighLighter::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); }
Althought it doesnt have error,but it become not work
The data.settings contained
20c6ff18f58e7fb7d4a2ca57f83468ac
but it doesnt work,like when i typed
True
False
say
ask
it doesnt change text's color -
@ELEMENTICY Please use debugger and see what happens...
-
@jsulm i used debugger,how to see the problem?
Im not often use Debugger, i dont know how to see what happened -
@ELEMENTICY Please learn how to use a debugger, this is a basic tool for a programmer.
Go through your code step by step in the debugger and see what your code does. -
Hi
You want to check out what does when it sees the True False say ask keywords.
See if it goes into the while loop or not as then we know if its due its not seeing the keywords
and if it does , it means that the color change is not working.start app in debug mode. ( the other run symbol) and place break point at this function. void MyHighLighter::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); }