📜 ⬆️ ⬇️

QScintilla: highlight syntax in application

Hi, $ username!

UPD : the second and third part of the cycle about QScintilla.

Today I want to tell you about a great project - QScintilla, which highlights the syntax of the code in Qt-applications. Often there is a need to highlight something. For example: C ++, Bash, PHP, Diff ... The list goes on and on. But here is the solution: the Scintilla port on Qt: QScintilla.
')
In this post I will explain how to install and use QScintilla in my applications using the example of Ubuntu Linux.

Installation


Let's start, perhaps, with the most important thing - with the installation. I propose to build QScintilla and install it in the system. To do this, download the tarball qscintilla . Unpack it in the folder we need. Now console. We carry out a couple of simple commands:

cd Qt4 qmake make sudo make install 

Excellent. If you did everything correctly, libqscintilla2.so will appear on your system.

Open Qt Creator


Hooray! You can start coding. Open creeitor - create a project. Now we add a line to the profile:

 LIBS += -lqscintilla2 

It is easy to guess that this line connects QScintilla. We now turn directly to the coding of the project. Constructor mainwindow.cpp:

 #include <QtGui> #include <Qsci/qsciscintilla.h> #include <Qsci/qscilexercpp.h> #include "mainwindow.h" MainWindow::MainWindow() { textEdit = new QsciScintilla; //   textEdit->setUtf8(true); //      setCentralWidget(textEdit); //    ui QsciLexerCPP * lexCpp = new QsciLexerCPP(this); //   ( ) textEdit->setLexer(lexCpp); //  ++    } 

Do not forget to add to MainWindow:

 private: QSciScintilla *textEdit; 


You can safely compile - we see an editor that highlights the C ++ code with a bang.



While it does not look very cool, but it is only for now. And now the fun begins. I propose to expand our editor and decorate it. We add in the designer:

  //!       textEdit->setCaretLineVisible(true); textEdit->setCaretLineBackgroundColor(QColor("gainsboro")); //!  textEdit->setAutoIndent(true); textEdit->setIndentationGuides(false); textEdit->setIndentationsUseTabs(true); textEdit->setIndentationWidth(4); //! margin   ,     breakpoints textEdit->setMarginsBackgroundColor(QColor("gainsboro")); textEdit->setMarginLineNumbers(1, true); textEdit->setMarginWidth(1, 50); //! -      textEdit->setAutoCompletionSource(QsciScintilla::AcsAll); textEdit->setAutoCompletionCaseSensitivity(true); textEdit->setAutoCompletionReplaceWord(true); textEdit->setAutoCompletionUseSingle(QsciScintilla::AcusAlways); textEdit->setAutoCompletionThreshold(0); //!    textEdit->setBraceMatching(QsciScintilla::SloppyBraceMatch); textEdit->setMatchedBraceBackgroundColor(Qt::yellow); textEdit->setUnmatchedBraceForegroundColor(Qt::blue); 


Now we get a relatively working editor, which even can do something. Of course, he has many flaws, but all of them are also fixable.



This is probably all. I would like to tell you more about writing my lexer, managing the search and monitoring strings, but this is too much for one article. But if you do not want to wait for the next article, read the documentation and most importantly - experiment!

Materials





Thank you for your attention, and good coding!

Source: https://habr.com/ru/post/144411/


All Articles