Search code examples
qtqt4folding

Code folding in a Qt widget subclassed from QPlainTextEdit?


I need to implement something like code folding feature of Qt Creator for my widget. Its a subclass of QPlainTextEdit with some additional syntax highlighting and line numbering. It should hide text between some environment declaration statement and when user requests show them again. Line numbering should not be affected. For example the text between .begin[signal] and .end[signal]. should be hidden when user clicks on the small ▼ placed on the thin vertical bar beside number lines, then clicking again on the ▶ will show text. (like Qt Creator, Eclipse and most of other IDEs)

The problem is that I can’t hide lines and find a way to manage line numbering display to avoid pitfall. Any suggestion?


Solution

  • Why not use QScintilla instead? It is a well-tried editor with the folding feature.

    For folding with your own rules you need to make your own lexer by subclassing QsciLexer of QsciLexerCustom. I suggest studying existing lexers. You will need to implement the same functionality for your rules even if you decide not to use QScintilla.