[Qt-interest] Match new line character

Israel Brewster israel at frontierflying.com
Fri Mar 27 00:52:02 CET 2009





On Mar 26, 2009, at 3:23 PM, Kaleb Pederson wrote:

> 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:

Oh, ok. That makes sense then. I guess I was just supposed to know  
that automatically or something then? I mean, \n is taught in basic c+ 
+ as the new line character, and Qt::Key_Enter and Qt::Key_Return were  
both listed in the Qt documentation (so I didn't even really need to  
know what they were), but that 8233 had me baffled. Oh well, now I  
know :-) Thanks!

-----------------------------------------------
Israel Brewster
Computer Support Technician II
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
>
>
> http://www.fileformat.info/info/unicode/char/2029/index.htm
>
> HTH,
>
> --Kaleb




More information about the Qt-interest-old mailing list