Qt開源編輯器qsciscintilla的一些用法


首先放一張自己做的軟件中的編輯器的效果圖

中間紅色的框就是放在Qt的tabwidget控件中的qsciscintilla編輯器

先從官網下載qsciscintilla源碼,在qtcreater中編譯,提取靜態庫和頭文件,將庫和Qsci中的頭文件添加到自己的項目的pro配置文件中,具體編譯方法可參考網上的帖子,這里不再贅述,可以運行之后再看下面的操作

1,一些常規設置,都是通過對應的函數來設置

//設置字體
QFont font("Courier", 10, QFont::Normal); ui->textEdit->setFont(font); ui->textEdit->setMarginsFont(font); QFontMetrics fontmetrics = QFontMetrics(font);
//設置左側行號欄寬度等 ui
->textEdit->setMarginWidth(0, fontmetrics.width("00000")); ui->textEdit->setMarginLineNumbers(0, true); ui->textEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch); ui->textEdit->setTabWidth(4);
//設置括號等自動補全 ui
->textEdit->setAutoIndent(true);
//初始設置c++解析器 ui
->textEdit->setLexer(new QsciLexerCPP(this)); //設置自動補全 ui->textEdit->setCaretLineVisible(true);
//設置光標所在行背景色 ui
->textEdit->setCaretLineBackgroundColor(Qt::lightGray); // ui->textEdit->setCursorPosition(2,2); //int markerDefine(MarkerSymbol sym, int markerNumber = -1); ui->textEdit->SendScintilla(QsciScintilla::SCI_SETCODEPAGE,QsciScintilla::SC_CP_UTF8);//設置編碼為UTF-8
//得到光標位置
int line,col;
ui->textEdit->getCursorPosition(&line,&col);

2,通過SendScintilla的參數來設置

最新版編輯器(QScintilla_gpl-2.11.1)好多設置都是通過QsciScintillaBase類中的SendScintilla函數來進行設置的,這個函數有多個重載:

    //! Send the Scintilla message \a msg with the optional parameters \a
    //! wParam and \a lParam.
    long SendScintilla(unsigned int msg, unsigned long wParam = 0,
            long lParam = 0) const;

    //! \overload
    long SendScintilla(unsigned int msg, unsigned long wParam,
            void *lParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, uintptr_t wParam,
            const char *lParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, const char *lParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, const char *wParam,
            const char *lParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, long wParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, int wParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, long cpMin, long cpMax,
            char *lpstrText) const;

    //! \overload
    long SendScintilla(unsigned int msg, unsigned long wParam,
            const QColor &col) const;

    //! \overload
    long SendScintilla(unsigned int msg, const QColor &col) const;

    //! \overload
    long SendScintilla(unsigned int msg, unsigned long wParam, QPainter *hdc,
            const QRect &rc, long cpMin, long cpMax) const;

    //! \overload
    long SendScintilla(unsigned int msg, unsigned long wParam,
            const QPixmap &lParam) const;

    //! \overload
    long SendScintilla(unsigned int msg, unsigned long wParam,
            const QImage &lParam) const;

在這個類的前面有大量的枚舉值,既是這個函數可以用到的參數,

大多數枚舉值都有英文注釋,可自己查找對應的參數,

這里只介紹我自己用到的幾個

//SCI_MARKERGET 參數用來設置標記,默認為圓形標記
int nMask = ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERGET,linenr-1); //SCI_MARKERSETFORE,SCI_MARKERSETBACK設置標記前景和背景標記 ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERSETFORE, 0,QColor(Qt::red)); ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERSETBACK, 0,QColor(Qt::red)); ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERADD,linenr-1);

效果如圖

下面設置下划線標記

ui->textEdit->SendScintilla(QsciScintilla::SCI_STYLESETUNDERLINE,linenr,true);
ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERDEFINE,0,QsciScintilla::SC_MARK_UNDERLINE)

效果如下

刪除所有標記

textEdit->SendScintilla(QsciScintilla::SCI_MARKERDELETEALL);

跳轉標記

//跳轉到下一個標記
void
QsciEditor::gotoNext()//函數寫完還未測試,大概是這個作用,可自行測試 { int line,col; ui->textEdit->getCursorPosition(&line,&col); ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERNEXT,line); }
//跳轉到上一個標記
void QsciEditor::gotoPre()
{
    int line,col;
    ui->textEdit->getCursorPosition(&line,&col);
    ui->textEdit->SendScintilla(QsciScintilla::SCI_MARKERPREVIOUS,line);
}

跳轉光標到行line,列index

void QsciEditor::setCursorPosition_p(int line,int index)
{
    ui->textEdit->setCursorPosition(line-1,index);
    ui->textEdit->setCaretLineBackgroundColor(Qt::lightGray);
    ui->textEdit->SendScintilla(QsciScintilla::SCI_SETFIRSTVISIBLELINE,line);
}

設置詞法分析器

QsciLexer *textLexer;//創建一個詞法分析器

//常用以下幾種,注意添加對應的頭文件 textLexer = new QsciLexerCPP; textLexer = new QsciLexerPython; textLexer = new QsciLexerJava; textLexer = new QsciLexerHTML; textLexer = new QsciLexerCSharp; textLexer = new QsciLexerCSS; textLexer = new QsciLexerJavaScript;

一些編輯操作函數,看函數名就知道是干嘛的了,手動滑稽

    ui->textEdit->undo();

    ui->textEdit->redo();

    ui->textEdit->copy();

    ui->textEdit->cut();

    ui->textEdit->paste();

    ui->textEdit->findFirst(expr,true,false,true,true);

    ui->textEdit->findNext();

    ui->textEdit->replace(replaceStr);

 常用的信號

//編輯器內容被編輯
textChanged()
//是否可復制,大概是這樣copyAvailable(bool)

就說這些,剩下的需要去源代碼里面找了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM