![]() |
Home · Examples |
The Code Editor example shows how to create a simple editor that has line numbers and that highlights the current line.
We implement the editor in CodeEditor, which is a widget that inherits QPlainTextEdit. We keep a separate widget in CodeEditor (LineNumberArea) onto which we draw the line numbers.
QPlainTextEdit inherits from QAbstractScrollArea, and editing takes place within its viewport()'s margins. We make room for our line number area by setting the left margin of the viewport to the size we need to draw the line numbers.
When it comes to editing code, we prefer QPlainTextEdit over QTextEdit because it is optimized for handling plain text. See the QPlainTextEdit class description for details.
QPlainTextEdit lets us add selections in addition to the the selection the user can make with the mouse or keyboard. We use this functionality to highlight the current line. More on this later.
We will now move on to the definitions and implementations of CodeEditor and LineNumberArea. Let's start with the LineNumberArea class.The LineNumberArea Class
We paint the line numbers on this widget, and place it over the CodeEditor's viewport()'s left margin area.
We need to use protected functions in QPlainTextEdit while painting the area. So to keep things simple, we paint the area in the CodeEditor class. The area also asks the editor to calculate its size hint.
Note that we could simply paint the line numbers directly on the code editor, and drop the LineNumberArea class. However, the QWidget class helps us to scroll() its contents. Also, having a separate widget is the right choice if we wish to extend the editor with breakpoints or other code editor features. The widget would then help in the handling of mouse events.
Missing snippet: widgets/codeeditor/codeeditor.h.
Missing snippet: widgets/codeeditor/codeeditor.h.
Whenever, the cursor's position changes, we highlight the current line in highlightCurrentLine().CodeEditor Class Implementation
We will now go through the code editors implementation, starting off with the constructor.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
In the constructor we connect our slots to signals in QPlainTextEdit. It is necessary to calculate the line number area width and highlight the first line when the editor is created.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
QPlainTextEdit gives the possibility to have more than one selection at the same time. we can set the character format (QTextCharFormat) of these selections. We clear the cursors selection before setting the new new QPlainTextEdit::ExtraSelection, else several lines would get highlighted when the user selects multiple lines with the mouse.
One sets the selection with a text cursor. When using the FullWidthSelection property, the current cursor text block (line) will be selected. If you want to select just a portion of the text block, the cursor should be moved with QTextCursor::movePosition() from a position set with setPosition().
Missing snippet: widgets/codeeditor/codeeditor.cpp.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
We get the top and bottom y-coordinate of the first text block, and adjust these values by the height of the current text block in each iteration in the loop.
Missing snippet: widgets/codeeditor/codeeditor.cpp.
In addition to line numbers, you can add more to the extra area, for instance, break points.
QSyntaxHighlighter gives the possibility to add user data to each text block with setCurrentBlockUserData(). This can be used to implement parenthesis matching. In the highlightCurrentLine(), the data of the currentBlock() can be fetched with QTextBlock::userData(). Matching parentheses can be highlighted with an extra selection.
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt Jambi 4.5.2_01 |