[Qt-interest] Match new line character

Kaleb Pederson kaleb.pederson at gmail.com
Fri Mar 27 00:23:36 CET 2009


On Thu, Mar 26, 2009 at 4:10 PM, Israel Brewster
<israel at frontierflying.com> wrote:
>
>
>
> On Mar 26, 2009, at 12:48 PM, Kaleb Pederson wrote:
>
>> 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.
>
> Thanks for the response. Unfortunately, using that signal leaves me with the
> same problem: I can't seem to figure out how to match a return or enter.

Since I have the code, here's essentially what I do:

    QString text = cur.selectedText();
    ushort uc = text[i].unicode();
    if (uc != 8233 && uc != 10 && uc != 13 )
    { /* do something for non-return characters */ }

Character 8233 is the unicode paragraph separator:

http://www.fileformat.info/info/unicode/char/2029/index.htm

HTH,

--Kaleb




More information about the Qt-interest-old mailing list