1

I have the simplest minimum working example to highlight just two lines. The two lines to be highlighted are shown below. These two lines are the only contents of the QTextEdit.


begin

end


Using QSyntaxHighlighter and QTextEdit, I am unable to highlight these two lines using QRegularExpression. The working code is shown below. When compiled using QtCreater, it gives zero warnings and runs fine. I am using Qt5.13 MinGW x64. The interesting point is that the RegEx I am using works fine in online checker like https://regex101.com/.

Ultimately I want to see that the two lines should appear in red color. The first line contains the word "begin" and the second line contains the word "end".* Please help.

mySyntaxHighlighter.h

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

signals:

public slots:

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

mySyntaxHighligher.cpp

#include "mysyntaxhighlighter.h"

mySyntaxHighlighter::mySyntaxHighlighter(QTextDocument *document) : QSyntaxHighlighter(document)
{

}

void mySyntaxHighlighter::highlightBlock(const QString &text) {
    QTextCharFormat citeFormat;
    citeFormat.setForeground(QColor(Qt::red));
    citeFormat.setFontFamily("Consolas");
    citeFormat.setFontPointSize(14);

    QRegularExpression regex = QRegularExpression(QStringLiteral("begin(.*\n)+end"));
    regex.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);

    QRegularExpressionMatchIterator i = regex.globalMatch(text);

    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        setFormat(match.capturedStart(), match.capturedLength(), citeFormat);
    }
}

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

    QTextEdit *te = new QTextEdit(this);
    te->setAcceptRichText(false);
    te->setFontFamily("Consolas");
    te->setFontPointSize(14);
    te->setMinimumSize(1200, 600);

    myHighlighter = new mySyntaxHighlighter(te->document());

    setCentralWidget(te);
    setMinimumWidth(1200);
    te->setFocus();
}

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;
};
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rasi
  • 11
  • 2
  • you are assuming that text is all the text but it is only a block, I have doubts about your requirement: if "begin" and "end" are on the same line, should they be highlighted? If there is only "begin" then the following text must be highlighted or must it necessarily be "end"? The last thing I ask because, for example, the C ++ editors have as a rule to highlight the comments the text just finds "/*" and finish it when there is "*/", for example a text could start with "/*" so if there is no "* /" all the text will be highlighted. – eyllanesc Jul 21 '19 at 08:15
  • What I mean is that all text in between "begin" and "end" should be highlighted. I am testing with very simple case when nothing is in between them. Also, as you said, when the "begin" and "end" are on the same line, it is highlighting. But that is not required because they will be on different lines. Your analogy is also correct, that is "/*" is equivalent to "begin" and "*/" is equivalent to "end". But the bottom line is that I am trying to implement with a single regex. – rasi Jul 21 '19 at 15:10
  • You can not with a single regex, check the second example provided by Qt: https://doc.qt.io/qt-5/qsyntaxhighlighter.html#details – eyllanesc Jul 21 '19 at 15:12
  • Thank you. So, if you confirm that we have to go for two regex, to solve the problem, then, I will discard this approach. The main attraction here is the option "DotMatchesEverythingOption". In addition, online regex checker work as expected. Thanks for your information. – rasi Jul 21 '19 at 15:20

0 Answers0