Please nominate your Qt Champions for 2021!

[SOLVED] Indent multiple lines with TAB in a QPlainTextEdit

  • Hi

    I am very new to Qt and my plan is to write a little text editor for a better understanding in Qt. But I have a little problem with the indentation and the TAB key.

    Is there an easy way to indent multiple (selected) lines with the tab key ? At the moment, it replaces the selected lines with a tab - example:

    Current situation:
    Line 1
    Line 2
    Line 3
    Line 4


    <Tab>Line 1
    <Tab>Line 2
    <Tab>Line 3
    <Tab>Line 4

    But the default behaviour is this, because the 4 lines are replaced with a tab (because they are selected):


    Is there a way to override this behaviour or to write an indent/outdent function ?


  • Hello Threadcore,
    You should subclass QPlainTextEdit and override following method:
    @virtual void QPlainTextEdit::keyPressEvent ( QKeyEvent * e )@

    Catch the tab key as described in
    Then you should iterate through selected lines and prepend tab character ('\t').
    You can obtain the selection through a QTextCursor object, which in turn you can obtain by method
    @QTextCursor QPlainTextEdit::textCursor () const@

  • Okay, thank you, that's a good starting point

    -PS: Is there a button to mark the thread as solved ?- Titel

Log in to reply