0

I have an ultra minimal working Qt code. I want to highlight the standard multi-line style C++ comment lines starting with "/" and ending with "/".

The Qt project compiles with zero warning and runs fine. But the highlighting takes place only on the characters and not full background region.

The expected behaviour is attached as png images. The code is based on Qt documentation version 5.13 with MinGw x64 compiler.

I also thank the stackoverflow contributor https://stackoverflow.com/users/6622587/eyllanesc for suggesting me to use the two regex to highlight.

Output of this code:

Only characters are highlighted

My Expected output:

I want the complete background to be highlighted

Complete working Code listing is as below. Please suggest and help. Thanks in advance.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
#include "mysyntaxhighlighter.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    mySyntaxHighlighter *myHighlighter;
    QTextEdit *te;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mysyntaxhighlighter.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    te = new QTextEdit(this);
    te->setAcceptRichText(false);
    te->setFontPointSize(16);
    myHighlighter = new mySyntaxHighlighter(te->document());
    setCentralWidget(te);
    setMinimumWidth(1200);
    te->setFocus();

    te->append("\n/*\nFirst line\nSecond line\nThird line\netc.\n*/\n");
}

MainWindow::~MainWindow()
{
    delete ui;
}

mySyntaxHighlighter.h

#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H

#include <QObject>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>

class mySyntaxHighlighter : public QSyntaxHighlighter
{
    Q_OBJECT
public:
    explicit mySyntaxHighlighter(QTextDocument *document);

private:
        QTextCharFormat f;
        QRegularExpression  regex1, regex2;
        QBrush  b;

signals:

public slots:

protected:
    void highlightBlock(const QString &text);
};

#endif // MYSYNTAXHIGHLIGHTER_H

mySyntaxHighlighter.cpp

#include "mysyntaxhighlighter.h"

mySyntaxHighlighter::mySyntaxHighlighter(QTextDocument *document) : QSyntaxHighlighter(document)
{
    b.setColor(Qt::yellow);
    b.setStyle(Qt::SolidPattern);
    f.setBackground(b);
    f.setForeground(Qt::blue);
    f.setFontFamily("Courier New");
    f.setFontPointSize(16);

    regex1 = QRegularExpression(QStringLiteral("/\\*"));
    regex2 = QRegularExpression(QStringLiteral("\\*/"));
}

void mySyntaxHighlighter::highlightBlock(const QString &text) {
    setCurrentBlockState(0);

    QRegularExpressionMatch match;
    int startIndex = 0;

    if (previousBlockState() != 1) {
        startIndex = text.indexOf(regex1);
    }

    while (startIndex >= 0) {
        match = regex2.match(text, startIndex);
        int endIndex = match.capturedStart();
        int Length = 0;
        if (endIndex == -1) {
            setCurrentBlockState(1);
            Length = text.length() - startIndex;
        }
        else {
            Length = endIndex - startIndex + match.capturedLength();
        }

        setFormat(startIndex, Length, f);
        startIndex = text.indexOf(regex1, startIndex + Length);
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rasi
  • 11
  • 2

1 Answers1

0

before to change text properties you have to create a cursor from your textDocument like

QTextCursor highlightCursor = QTextCursor(document)

then put a format to your cursor

QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(QColor(Qt::yellow));

With this cursor you can move around the document and manipulate texts

highlightCursor = document->find(regexp,highlightCursor)

A good example you can find here textfinder example

By the way, I think, its a better way to use only one regexp like

\/\*.*?\*\\s

which should match the whole comment

Alfredo cubitos
  • 161
  • 2
  • 5
  • Thank for your suggestions. Unfortunately, once again, the use of QTextCursor(document) also highlights only the text region. But I am interested to highlight the full background as shown in my quested. Still waiting for some other way around. – rasi Aug 01 '19 at 11:47
  • ok, in this case I would use [Richtext editing.]( https://doc.qt.io/qt-5/richtext.html). So you can put the result into a paragraph tag and add a background style like

    result

    – Alfredo cubitos Aug 02 '19 at 08:03