QScintilla2 wraps the Scintilla control written by Neil Hodgson for the Qt4 toolkit.
It comes with a variety of predefined lexers, you may add your own ones, but you'll have to write them in C (see the Scintilla docs for that). Note that the QTextEdit widget can be a good alternative coupled with a QHighlighter, but you'll then miss the line numbers, the code folding and much more.
You can list all the avaible lexers this way for example :
import PyQt4.Qsci
langs = [i for i in dir(PyQt4.Qsci) if i.startswith('QsciLexer')]
for i,l in enumerate(langs):
print i,l[9:] # we don't need to print "QsciLexer" before each name
At the time of writing this article, you can choose between 23 lexers :
- ---> Nothing : interpret it as the Null Lexer
- Bash
- Batch
- CMake
- CPP
- CSS
- CSharp
- D
- Diff
- HTML
- IDL
- Java
- JavaScript
- Lua
- Makefile
- POV
- Perl
- Properties
- Python
- Ruby
- SQL
- TeX
- VHDL
The following code snippet opens a window and loads his own source code, so the lexer his the Python one.

- show line numbers ;
- show a fold margin ;
- customize those margins (foreground/background colors and fonts);
#!/usr/bin/env python
# -*- coding: latin1 -*-
"""
Basic use of the QScintilla2 widget
Note : name this file "qt4_sci_test.py"
"""
import sys
from PyQt4.QtGui import QApplication
from PyQt4 import QtCore, QtGui
from PyQt4.Qsci import QsciScintilla, QsciScintillaBase, QsciLexerPython
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = QsciScintilla()
## define the font to use
font = QtGui.QFont()
font.setFamily("Consolas")
font.setFixedPitch(True)
font.setPointSize(10)
# the font metrics here will help
# building the margin width later
fm = QtGui.QFontMetrics(font)
## set the default font of the editor
## and take the same font for line numbers
editor.setFont(font)
editor.setMarginsFont(font)
## Line numbers
# conventionnaly, margin 0 is for line numbers
editor.setMarginWidth(0, fm.width( "00000" ) + 5)
editor.setMarginLineNumbers(0, True)
## Edge Mode shows a red vetical bar at 80 chars
editor.setEdgeMode(QsciScintilla.EdgeLine)
editor.setEdgeColumn(80)
editor.setEdgeColor(QtGui.QColor("#FF0000"))
## Folding visual : we will use boxes
editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)
## Braces matching
editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)
## Editing line color
editor.setCaretLineVisible(True)
editor.setCaretLineBackgroundColor(QtGui.QColor("#CDA869"))
## Margins colors
# line numbers margin
editor.setMarginsBackgroundColor(QtGui.QColor("#333333"))
editor.setMarginsForegroundColor(QtGui.QColor("#CCCCCC"))
# folding margin colors (foreground,background)
editor.setFoldMarginColors(QtGui.QColor("#99CC66"),QtGui.QColor("#333300"))
## Choose a lexer
lexer = QsciLexerPython()
lexer.setDefaultFont(font)
editor.setLexer(lexer)
## Render on screen
editor.show()
## Show this file in the editor
editor.setText(open("qt4_sci_test.py").read())
sys.exit(app.exec_())
More to come later...