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. QSyntaxHighlighter::highlightBlock highlights previous and current block
Forum Updated to NodeBB v4.3 + New Features

QSyntaxHighlighter::highlightBlock highlights previous and current block

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 1 Posters 303 Views
  • 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.
  • A Offline
    A Offline
    added
    wrote on last edited by
    #1

    Implementation:

    CustomHighlighter::CustomHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {
    }
     
    void CustomHighlighter::setHighlighterFormat(const QTextCharFormat &format) {
        this->format = format;
    }
     
    void CustomHighlighter::highlightBlock(const QString &text) {
    //    if(text.isEmpty() || currentBlockState() == 1)
    //        return;
        setFormat(0, text.length(), format);
    //    setCurrentBlockState(1);
    }
     
    Logger::Logger(QWidget *parent) : QPlainTextEdit(parent) {
        setMaximumBlockCount(50);
        highlighter = new CustomHighlighter(document());
    }
     
    Logger::~Logger() {
        delete highlighter;
    }
     
    void Logger::setFormat(const QTextCharFormat &format) {
        highlighter->setHighlighterFormat(format);
    }
    

    Next is a timer slot, just for example:

    QTextCharFormat format;
    format.setFontWeight(250);
    int x = random() % 3;
    switch(x) {
    case 0:
        format.setForeground(Qt::red);
        break;
    case 1:
        format.setForeground(Qt::green);
        break;
    case 2:
        format.setForeground(Qt::blue);
        break;
    }
     
    logger->setFormat(format);
    QDateTime date = QDateTime::currentDateTime();
    logger->appendPlainText(date.toString() + "\n");
    

    This works fine. But if new line character is removed both current appended and previous line (block) are highlighted.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      added
      wrote on last edited by
      #2

      So I added enum to check block states:

       enum { FORMATTED = 1, LAST = 2 };
      

      Then made changes in highlightBlock:

      void CustomHighlighter::highlightBlock(const QString &text) {
          if(text.isEmpty() || currentBlockState() == LAST)
              return;
          if(currentBlockState() == FORMATTED) {
              setFormat(0, text.length(), previousFormat);
              setCurrentBlockState(LAST);
          } else {
              setFormat(0, text.length(), format);
              previousFormat = format;
              setCurrentBlockState(FORMATTED);
          }
      }
      

      This has one problem. After maximum blocks are appended the top most block "looses" format.
      But anyway it seems that I am overdoing it. And why is formatted text returned to highlightBlock?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        added
        wrote on last edited by
        #3

        Okay, this works:

        void CustomHighlighter::highlightBlock(const QString &text) {
            if(text.isEmpty())
                return;
            int start = 0;
            if(currentBlockState() == LAST) {
                BlockFormat block = queue.dequeue();
                QTextCharFormat lastFormat = block.first;
                int length = block.second;
                setFormat(start, length, lastFormat);
            }
            else if(currentBlockState() == FORMATTED) {
                setFormat(start, text.length(), previousFormat);
                setCurrentBlockState(LAST);
                queue.enqueue(BlockFormat(format, text.length()));
            } else {
                setFormat(start, text.length(), format);
                previousFormat = format;
                setCurrentBlockState(FORMATTED);
            }
        }
        

        Where BlockFormat is:

        typedef QPair<QTextCharFormat, int> BlockFormat;
        

        Question is same, am I overdoing it? Is this the simplest solution?

        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