前提条件:
-
官网下载Qscintilla库,并使用QtCreator构建出静态库。
-
将Qsci文件夹和生成的库拷到准备使用的项目文件下
正文
-
实现自己的customqscintilla
// 自动缩进 editor->setTabWidth(4); editor->setIndentationWidth(4); editor->setIndentationsUseTabs(false); editor->setAutoIndent(true); // 当前行高亮 editor->setCaretLineVisible(true); editor->setCaretLineBackgroundColor(QColor("#E8E8FF")); editor->setCaretWidth(3); // 词法分析器 auto textLexer = new QsciLexerMat(editor); editor->setLexer(textLexer); // 行号 editor->setMarginLineNumbers(1, true); editor->setMarginWidth(1, "000"); // 代码折叠 editor->setFolding(QsciScintilla::BoxedTreeFoldStyle); editor->setMarginSensitivity(2, true); // 确保可以点击边距进行折叠 editor->setMarginType(2, QsciScintilla::SymbolMargin); auto api = new QsciAPIs(textLexer); // 读取关键字文件 if (!api->load("C:/Users/12117/Desktop/QtWidgetsApplication1/QtWidgetsApplication1/QtWidgetsApplication1/apis.txt")) { QMessageBox::warning(this, "Warning", "Load Failed"); } else { api->prepare(); } // 自动补全 editor->setAutoCompletionSource(QsciScintilla::AcsAll); editor->setAutoCompletionCaseSensitivity(true); editor->setAutoCompletionReplaceWord(true); editor->setAutoCompletionUseSingle(QsciScintilla::AcusNever); editor->setAutoCompletionThreshold(1)
-
继承QsciLexerCustom实现自己的自定义词法分析器
class QsciLexerMat : public QsciLexerCustom { Q_OBJECT public: QsciLexerMat(QObject* parent); ~QsciLexerMat(); virtual void styleText(int start, int end); virtual const char* language() const; virtual QString description(int style) const; QColor defaultColor(int style) const; virtual const char* keywords(int set) const; enum { SCE_C_DEFAULT = 0, SCE_C_COMMENTLINE, SCE_C_STRING, SCE_C_NUMBER, SCE_C_KEYWORD, SCE_C_IDENTIFIER }; };
-
重点实现styleText函数(以下代码仅作示例)
///----省略----//// auto handleString = [&]() { QChar delimiter = text[pos]; styleBuffer[pos] = SCE_C_STRING; ++pos; while (pos < length) { if (text[pos] == '\\') { // 转义字符 if (pos + 1 < length) { styleBuffer[pos] = SCE_C_STRING; styleBuffer[pos + 1] = SCE_C_STRING; pos += 2; } else { break; } } else if (pos < length && text[pos] == delimiter) { styleBuffer[pos] = SCE_C_STRING; ++pos; inString = false; break; } else if (pos < length) { styleBuffer[pos] = SCE_C_STRING; ++pos; } else { break; } } }; ///----省略----//// while (pos < length) { QChar ch = text.at(pos); // 处理字符串,注释,关键字和标识符,数字 if (!inString && (ch == '\'' || ch == '"')) { inString = true; handleString(); continue; } ////----省略----//// // 应用样式 startStyling(start); int currentBytePos = start; int currentStyle = -1; int count = 0; QTextCodec* codec = QTextCodec::codecForName("UTF-8"); if (!codec) { qWarning("Failed to get UTF-8 codec"); return; } QByteArray byteArray; for (int i = 0; i < styleBuffer.size(); ++i) { if (styleBuffer[i] != currentStyle) { if (count > 0) { byteArray = codec->fromUnicode(text.mid(i - count, count)); setStyling(byteArray.length(), currentStyle); // 设置相同样式的字符 currentBytePos += byteArray.length(); } currentStyle = styleBuffer[i]; count = 0; } count++; } if (count > 0) { byteArray = codec->fromUnicode(text.mid(text.length() - count, count)); setStyling(byteArray.length(), currentStyle); // 设置最后一段相同样式的字符 currentBytePos += byteArray.length(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述