Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Syntax highlighting with a gradient background
Forum Updated to NodeBB v4.3 + New Features

Syntax highlighting with a gradient background

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tmalfrere
    wrote on last edited by
    #1

    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);
    }
    

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tmalfrere
          wrote on last edited by
          #4

          @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...

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            "The bug report system":http://bugreports.qt-project.org

            Don't forget to check it's something already known

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved