Syntax highlighting with a gradient background
-
Hi,
I've created a syntax highlighter that should display lines with "error" with a red gradient background and all lines with "warning" with a yellow gradient background.
I feed the document from a QPlainTextEdit to my highlighter and everything works except that half the time the gradients are not gradient but solid colors.
Am I doing something wrong or do I have a bug?
here's my highlighter class I derived from QSyntaxHighlighter()
@
class QLseSyntaxHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
explicit QLseSyntaxHighlighter(QObject *parent = 0);
explicit QLseSyntaxHighlighter(QTextDocument *parent = 0);
void highlightBlock(const QString &text);signals:
public slots:
private:
QTextCharFormat m_FormatWarning;
QTextCharFormat m_FormatError;QRegExp m_ExpressionForWarning; QRegExp m_ExpressionForError;
};
QLseSyntaxHighlighter::QLseSyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
QString ErrorPattern(QLatin1String("^.error.$"));
m_ExpressionForError = QRegExp(ErrorPattern,Qt::CaseInsensitive);QString WarningPattern(QLatin1String("^.*warning.*$")); m_ExpressionForWarning = QRegExp(WarningPattern, Qt::CaseInsensitive); m_FormatError = QTextCharFormat(); m_FormatError.setFontWeight(QFont::Bold); QFont ErrorFont(m_FormatError.font()); QFontMetrics Errorfm(ErrorFont); QLinearGradient GradientError(0,Errorfm.height(),0,0); GradientError.setColorAt(1, QColor(255, 224, 224, 255)); GradientError.setColorAt(0, QColor(255, 32, 32, 255)); QBrush BrushError(GradientError); m_FormatError.setBackground(BrushError); m_FormatWarning = QTextCharFormat(); m_FormatWarning.setFontWeight(QFont::Bold); QFont WarningFont(m_FormatWarning.font()); QFontMetrics fm(WarningFont); QLinearGradient GradientWarning(0,fm.height(),0,0); GradientWarning.setColorAt(1, QColor(255, 253, 239, 255)); GradientWarning.setColorAt(0, QColor(255, 224, 0, 255)); QBrush BrushWarning(GradientWarning); m_FormatWarning.setBackground(BrushWarning);
}
void QLseSyntaxHighlighter::highlightBlock(const QString &text)
{
int index;//WriteLog("highlightBlock\n"); //WriteLog(text); //WriteLog("\n"); index = text.indexOf(m_ExpressionForError); while (index >= 0) { int length = m_ExpressionForError.matchedLength(); setFormat(index, length, m_FormatError); index = text.indexOf(m_ExpressionForError, index + length); } index = text.indexOf(m_ExpressionForWarning); while (index >= 0) { int length = m_ExpressionForWarning.matchedLength(); setFormat(index, length, m_FormatWarning); index = text.indexOf(m_ExpressionForWarning, index + length); }
}
@ -
Hey there!
Most probably, Qt just doesn't support this sort of behavior. I don't have an opportunity to check it now, but I am hardly sure that documentation says somewhere something like "you can't use gradient-based brushes for text format backgrounds".
Still not sure, lemme see.
-
Hi,
You might have discovered something. What version of Qt are you using ? On what OS ?
Here it look's like only the first line of the QTextDocument gets the proper gradient.
-
@tuknak: well, it does work 30% of the time...
@SGalst: I'm using Qt 5.2.1
I do have created a small test application that clearly demonstrates the problem, but I don't no where to post it...
-
"The bug report system":http://bugreports.qt-project.org
Don't forget to check it's something already known