Results 1 to 3 of 3

Thread: Pre-processing entered text in QTextDocument

  1. #1
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Pre-processing entered text in QTextDocument

    I want something like the following example:
    User start to type in QTextDocument "abc" I want change it to "ABC" and send it to QTextDocument so all other part of QTextDocument API like syntax-highlighting and text layouting should get "ABC" and not "abc".

    I didn't find a method about this so I used QTextDocument::contentsChange() signal to detect new added chars and then preprocess them (in this example apply toUpper()) but this is dirty and QAbstractTextDocumentLayout::documentChanged() will be called twice.

    Any idea?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Pre-processing entered text in QTextDocument

    I think you are mixing up "Replace All" and "Auto Correction/Formatting" features, better to implement them separately.

    Quote Originally Posted by srazi View Post
    I didn't find a method about this so I used QTextDocument::contentsChange() signal...
    Method to find and replace: Use toPlainText(), replace the text and then it back setPlainText(). If using this inside a slot connected to contentsChange() signal, take care to blockSignals(true), and blockSignals(false) guard statements while setting the new text to avoid recursion.


    Qt Code:
    1. MyTextDocument::on_contentsChange()
    2. {
    3. blockSignals(true);
    4. QString text = toPlainText();
    5. // find and replace in text
    6. setPlainText(text);
    7. blockSignals(false);
    8. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pre-processing entered text in QTextDocument

    Thanks for your reply. But as I want do this pre-processing on the fly, using "setPlainText(text)" seems to be slow on large documents.

Similar Threads

  1. QTextDocument layout problem with text disappears
    By 'milimoj in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2015, 10:10
  2. Prevent text from selecting in QTextDocument
    By 'milimoj in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2015, 21:41
  3. Replies: 1
    Last Post: 9th April 2011, 22:27
  4. How to draw columns text in QTextDocument?
    By nifei in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 03:17
  5. Vertical text in a QTextDocument
    By Angelo Moriconi in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2008, 06:30

Tags for this Thread

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.