Add two new actions, Jndent and Unindent, with the shortcuts Ctrl+] and Ctrl+[. Suitable icons are provided in the images subdirectory, and are already in the resources.qrc file. Both actions should be added to the Edit menu and to the edit toolbar. Implement the methods editIndent() and editUnindent(). They should indent or unindent the current line by inserting or removing four spaces at the beginning of the line, no matter where the insertion point is in the line. At the end, the insertion point should be at the same relative position as it was before the indent or unindent. The actions should be instance variables and should be enabled only if the document is not empty.
Make sure that you use QTextCursor.beginEditBlock() and QTextCursor.endEdit-Block() so that the indent or unindent can be undone as a single action—QText-Edit supports Ctrl+Z for undo. The two methods can be written in a total of about 20 lines.
If you want to achieve something more ambitious, try extending the two methods so that if there is a selection, the indent or unindent is applied to all the lines in the selection. This will add about another 40 lines, and it is slightly tricky. Make sure that at the end the original selection is in place.
You will need to read the documentation for QTextCursor and, especially, the anchor(), position(), setPosition(), and movePosition() methods.
A solution is provided in chap13/pythoneditor_ans.pyw.
• Using the Convenience Item Widgets
• Creating Custom Models
• Creating Custom Delegates
Was this article helpful?
Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.