Results 1 to 3 of 3

Thread: Problem with auto indent text while typing

  1. #1
    Join Date
    Apr 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with auto indent text while typing

    I'm trying to implement in a project an auto indent feature. Headings and lists should automatically indented while you typing. I have done this in a QTextEdit subclass and this approach works but it makes Undo/Redo non functional from the QTextEdit. I found out that textCursor().setBlockFormat is the problem but without the text wouldn't indented. Can anybody help me with this?

    Here the source of the keyPressEvent and here a full working example.

    Qt Code:
    1. void subedit::keyPressEvent( QKeyEvent *event )
    2. {
    3. QTextEdit::keyPressEvent( event );
    4.  
    5. QString text = textCursor().block().text();
    6. QTextBlockFormat textBlockFormat = textCursor().blockFormat();
    7.  
    8. int indent( 1 );
    9. if ( text.startsWith( "#" ) )
    10. indent = 0;
    11. else if ( text.indexOf( QRegularExpression( "(^[-*+] )|(^[\\d+]\\. )" ) ) == 0 )
    12. indent = 2;
    13.  
    14. textBlockFormat.setIndent( indent );
    15. textCursor().setBlockFormat( textBlockFormat );
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with auto indent text while typing

    Why does it make undo/redo non-functional? What is the behaviour you observe?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with auto indent text while typing

    When you insert some returns, go back to top of the document and write some text and press then several times CTRL+Z. All what you get is that the cursor goes one line down when you press the shortcut (the same way as you use the arrow down key), but no undo. Redo (CTRL+SHIFT+Z) has absolutely no effect, the cursor keeps at the current position.

Similar Threads

  1. Replies: 1
    Last Post: 13th April 2012, 09:05
  2. Replies: 9
    Last Post: 30th November 2011, 12:44
  3. text indent with wrap
    By linuxsong in forum Qt Quick
    Replies: 1
    Last Post: 26th July 2011, 14:14
  4. Qt Creator How can I modify auto-indent format ?
    By cionci in forum Qt Tools
    Replies: 1
    Last Post: 4th March 2010, 09:49
  5. Auto text select in QTableWidgetItem
    By tstankey in forum Newbie
    Replies: 2
    Last Post: 5th October 2006, 20:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.