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);
}
}
result
– Alfredo cubitos Aug 02 '19 at 08:03