【问题标题】:Custom syntax highlighting using QScintilla with Qt/C++使用 QScintilla 和 Qt/C++ 自定义语法高亮
【发布时间】:2021-09-27 21:46:46
【问题描述】:

我正在尝试使用 QScintilla 在 Qt (C++) 中实现 custom 语法突出显示,但是文档有点差。我搜索了一下,只找到了 PyQt 的教程(qscintilla.com)。我使用的是 C++ 而不是 Python。

那么我可以从哪里开始呢?我注意到有一个类 QSciLexCustom 但它看起来对我来说真的很混乱。

实际上,我的自定义语法与 C++ 非常相似,不同之处之一是在变量前使用$

【问题讨论】:

    标签: c++ qt syntax-highlighting qscintilla


    【解决方案1】:

    您可以继承QsciLexerCustom 并实现styleText() 函数:

    class MyCustomLexer: public QsciLexerCustom
    {
    public:
        MyCustomLexer(QsciScintilla *parent);
    
        void styleText(int start, int end);
    
        QString description(int style) const;
        const char *language() const;
    
        QsciScintilla *parent_; 
    };
    
    MyCustomLexer::MyCustomLexer(QsciScintilla *parent)
    {
        setColor(QColor("#000000"), 1);
        setFont(QFont("Consolas", 18), 1);
    }
    
    void MyCustomLexer::styleText(int start, int end)
    {
        QString lexerText = parent_->text(start, end);
        ...
    
        startStyling(...);
        setStyling(..., 1); // set to style 1
    }
    
    QString MyCustomLexer::description(int style) const
    {
        switch (style)
        {
        case 0:
            return tr("Default");   
    ...
    }
    
    const char *MyCustomLexer::language() const
    {
        return "MyCustomLexer";
    }  
    
    

    【讨论】:

    • 哦,非常感谢!这是一个很好的开始。看来这之后需要参考Scintilla的详细文档了。
    猜你喜欢
    • 2016-12-30
    • 2011-09-03
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多