собственно не пойму почему не работает простенький подсветчик:
OutputHighlighter.h#ifndef OUTPUT_HIGHLIGHTER_H_
#define OUTPUT_HIGHLIGHTER_H_
#include <QSyntaxHighlighter>
#include <QRegExp>
#include <QVector>
#include <QTextCharFormat>
class QString;
class QTextDocument;
class OutputHighlighter : public QSyntaxHighlighter {
Q_OBJECT
public:
OutputHighlighter(QTextDocument* parent = 0);
protected:
virtual void highlightBlock(const QString& text);
private:
struct rule {
QRegExp pattern;
QTextCharFormat format;
};
QVector<rule> rules;
QTextCharFormat warningFormat;
QTextCharFormat errorFormat;
};
#endif /* OUTPUT_HIGHLIGHTER */
OutputHighlighter.cpp#include "OutputHighlighter.h"
#include <QtGui>
OutputHighlighter::OutputHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
rule r;
warningFormat.setForeground(Qt::yellow);
errorFormat.setForeground(Qt::red);
r.pattern = QRegExp("\\bwarning\\b");
r.format = warningFormat;
rules.append(r);
r.pattern = QRegExp("\\berror\\b");
r.format = errorFormat;
rules.append(r);
}
void OutputHighlighter::highlightBlock(const QString& text) {
foreach(rule r, rules) {
QRegExp exp(r.pattern);
int index = text.indexOf(text);
while(index >= 0) {
int length = exp.matchedLength();
setFormat(index, length, r.format);
index = text.indexOf(exp, index + length);
}
}
}
Сообщение отредактировал void* - 20.1.2009, 14:14