DocumentDemo Example

#ifndef MDICHILD_H
#define MDICHILD_H

#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
#include <QTextEdit>

class QTextDocument;

/* Highlighter */
class Highlighter : public QSyntaxHighlighter
{
    Q_OBJECT
public:
    Highlighter(QTextDocument *parent = nullptr);
protected:
    void highlightBlock(const QString &text) override;
private:
    struct HighlightingRule
    {
        QRegularExpression pattern;
        QTextCharFormat format;
    };
    QList<HighlightingRule> highlightingRules;

    QRegularExpression commentStartExpression;
    QRegularExpression commentEndExpression;

    QTextCharFormat keywordFormat;
    QTextCharFormat classFormat;
    QTextCharFormat singleLineCommentFormat;
    QTextCharFormat multiLineCommentFormat;
    QTextCharFormat quotationFormat;
    QTextCharFormat functionFormat;
};

/* MdiChild */
class MdiChild : public QTextEdit
{
    Q_OBJECT
public:
    MdiChild();
    virtual ~MdiChild();
public:
    void newFile();
    bool loadFile(const QString &fileName);
    bool save();
    bool saveAs();
    bool saveFile(const QString &fileName);
    QString userFriendlyCurrentFile();
    const QString& currentFileName() const { return m_currentFileName;  }
protected:
    virtual void closeEvent(QCloseEvent *event);
private Q_SLOTS:
    void updateDocumentModified(bool modified);
private:
    bool aboutToBeClosed();
    void setCurrentFile(const QString &fileName, bool titled);
private:
    Highlighter* m_highlighter;
    QString m_currentFileName;
    bool m_untitled;
};

#endif