[Qt-interest] Match new line character

Kaleb Pederson kaleb.pederson at gmail.com
Thu Mar 26 21:48:52 CET 2009


On Thu, Mar 26, 2009 at 11:57 AM, Israel Brewster
<israel at frontierflying.com> wrote:
> I have a QTextEdit widget in my app where I need to watch what the
> user types, and if they type certain characters I need to respond in
> various ways. What I have done so far is connected a slot to the
> textChanged() signal, such that whenever they type a character I use
> the QTextCursor to select the last character (whatever they just
> typed, obviously) and compare it to the set of "action" characters.
> This works fine for normal characters (if there is a better way, I'd
> love to hear it) but it doesn't appear to work if they type a new line
> (return or enter, I don't care which). I tried comparing to "\n", but
> the comparison always fails. In the debugger, the selected string
> looks like "\e2\80\a9" in this case. I was thinking maybe that was
> just random junk, except that I always get that same string. How can I
> match on a new line? Thanks.

QTextDocument, which is accessible through the QTextEdit, has a
contentsChange signal that is emitted and provides sufficient
information to perform edits, even of pasted content. It's early
enough in the chain that the change will not have been drawn, making
the removal of characters appear as if it was never accepted.

You can use the arguments of the contentsChange signal to figure out
what characters, including newlines, may have been typed.  I've done
almost exactly this before.

HTH,

--Kaleb



More information about the Qt-interest-old mailing list