Qt Qsyntaxhighlighter给QTextEdit设置丰富的语法高亮格式

本文介绍如何使用Qt的QSyntaxHighlighter类为QTextEdit创建丰富的语法高亮显示,涵盖关键字、函数、注释、@{}等元素。通过设置不同的格式和规则,实现对代码的智能高亮,提升代码阅读体验。
摘要由CSDN通过智能技术生成
/*语法高亮
 * 
 *包括关键字、函数、注释、@{}、全局变量等等
 */

#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
//#include <QObject>
//#include <QRegExp>
#include <QTextEdit>
//#include <QVector>
//#include <QString>
//#include <global_operator.h>

QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE


class MySyntaxHighlighter:public QSyntaxHighlighter

{
    Q_OBJECT
public:
    MySyntaxHighlighter(QTextDocument *parent = 0);
    void getKeywords(QString path,QTextEdit* show);//从系统库路径下获取关键字列表,返回信息到消息框
    void getFuncwords(QString path,QTextEdit* show);//从用户库目录下获取函数列表,返回信息到消息框

public slots:
    void setGlobalListQueue(const QStringList& textQueue);//设置全局变量的高亮规则

protected:
    void highlightBlock(const QString  &text) Q_DECL_OVERRIDE;
private:
    struct HighlightingRule
    {
        QRegExp pattern;
        QTextCharFormat format;
    };

    void setNormalTextFormat();
    void setDigitalFormat();
    void setKeyAndFuncFormat();
    void setAtFormat();
    void setGlobalFormat();
    void setStringFormat();
    void setCommentFormat();


//    QString kPath;//好像暂时没什么用
//    QString fPath;

    void setGlobalListQueue();
    QVector<HighlightingRule> highlightingRules;//其它放在一个矢量里面
    QVector<HighlightingRule> globalHighlightRules;//全局变量会时刻变动,不跟其它语法高亮共用一个矢量
    QVector<HighlightingRule> atHighlightRules;//艾特的单独存一个矢量里面

    //这四个称作其它变量
    QTextCharFormat normalTextFormat;//普通文本的样式
    QTextCharFormat digitalFormat;//数字样式
    QTextCharFormat keywordFormat;//关键字高亮规则
    QTextCharFormat functionFormat;//函数高亮

    QTextCharFormat globalVarFormat;//全局变量高亮

    QTextCharFormat atFormat;//@{}式子高亮

    //这s三个不需要矢量
    QTextCharFormat commentFormat;//注释高亮
    QTextCharFormat multiLineCommentFormat;//块状注释高亮
    QTextCharFormat stringFormat;//字符串高亮

    //枚举块状注释开始、块状注释结束、单行注释和字符串
    enum symbolType{nothing = -1,comBegin = 0,singleCom,aString};
    enum symbolType getFirstSymbolTypeOf(QString tContext, int tIndex );//判断tContext中,从tIndex开始,最先出现哪个符号

    //如果where在有效区块结束符号*)后面(where>0)或者在previousBlockState=0的行(where=0),则对后面进行一系列检查
    void checkFromHere(QString text, int where);
    //如果区块在where开始,则对后面进行一系列检查
    void blockFromHere (QString text, int where);

//    int startIndex;
//    int pos;
    //检查行末的状态
//    bool lastCharInSingle = false;
//    bool lastCharInBlock = false;
//    bool lastCharInNormal = false;//既不属于字符串又不属于注释
};

#endif // MYSYNTAXHIGHLIGHTER_H








#include <QtGui>
#include <QRegExp>
#include <QFont>
#include <QFile>
//#include <algorithm>

#include "mysyntaxhighlighter.h"
//#include <QDialog>
//#include <QMessageBox>
//#include <QApplication>
//#include <queue&g
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QT Creator中给QTextEdit动态赋值,你可以使用QTextEdit的setText()函数来设置文本内容。以下是一个示例: 1. 在QT Creator中创建一个新的QT项目。 2. 在用户界面设计器中,将一个QTextEdit控件拖放到窗口中。 3. 在窗口类的头文件中添加必要的头文件和成员变量: ```cpp #include <QMainWindow> #include <QTextEdit> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; private slots: void updateText(); }; ``` 4. 在窗口类的源文件中实现构造函数、析构函数和更新文本的槽函数: ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::updateText); } MainWindow::~MainWindow() { delete ui; } void MainWindow::updateText() { QString dynamicText = "This is dynamically assigned text."; ui->textEdit->setText(dynamicText); } ``` 5. 在QT Creator的设计器中,添加一个按钮控件,并将其连接到`updateText()`槽函数。 6. 构建并运行应用程序,当你点击按钮时,QTextEdit控件将动态显示文本内容。 以上示例中,我们在`updateText()`槽函数中创建了一个QString类型的变量`dynamicText`,并将其赋值为所需的动态文本。然后,我们使用`setText()`函数将该文本设置QTextEdit控件的内容。 请注意,这只是一个简单的示例来演示如何动态给QTextEdit控件赋值。你可以根据自己的需求进行调整和修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值